从分析查询块返回UIImage数组 [英] Return UIImage Array From Parse Query Block

查看:112
本文介绍了从分析查询块返回UIImage数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能从这个函数返回[UIImage?]。 getDataInBackgroundWithBlock 不会让我在中设置 Void之外的返回值。但是,该块会在迭代时添加到 iconArray 中。但是一旦在块之外,数组又是空的。您将在下面的代码中看到数组所在的注释,并且不能正确打印。

调用连接到数据库,所有数据都在流动。这只是返回那个挂断的数组。

  class callData {


func queryImages() - > [UIImage?] {

var iconArray:[UIImage?] = []

var query:PFQuery = PFQuery(className:QuestionMaster)
query。 findObjectsInBackgroundWithBlock {(objects:[AnyObject] ?, error:NSError?) - >在对象中的

中无效! {

让imageFiles = object [questionImage] as! PFFile

imageFiles.getDataInBackgroundWithBlock({
(imageData:NSData?,error:NSError?) - >无效
if(error == nil){

let image = [UIImage(data:imageData!)] $ b $ iconArray + = image //将项目正确添加到数组
$ b println(iconArray)//在这里打印正确的数组

})// getDataInBackgroundWithBlock close

println(iconArray)//不在这里打印正确数组

} // for -loop close



return iconArray //返回空数组



$ b


解决方案

由于你的函数声明块在后台执行。
这意味着它将在后台加载,但也将继续其余的功能,从而返回一个空的数组。

为了解决这个问题,你应该在后台使用一个完成处理程序。

code> func queryImages(onComplete:(images:[UIImage?]) - > Void){
var iconArray:[UIImage?] = []
var query:PFQuery = PFQuery(className:QuestionMaster)
query.findObjectsInBackgroundWithBlock {(objects:[AnyObject]?,错误:NSError?) - >在对象中的

中无效! {

让imageFiles = object [questionImage] as! PFFile

let imageData = imageFiles.getData()
let image = UIImage(data:imageData!)
iconArray.append(image)

println (iconArray)
}
onComplete(images:iconArray)
}
}

尽管未经测试的上面的代码应该可以工作

for循环中的异步数据检索已被替换为有利于同步调用的缓解项目


I cannot get a [UIImage?] return from this function. The getDataInBackgroundWithBlock won't let me set a return value other than Void in. However, that block does add to the iconArray as it iterates through. But once outside of the block the array is empty again. You will see in the code below the comments where the array does and does not print correctly.

The call does connect to the DB, all data is flowing. It's simply returning that array that is the hang up.

class callData {


func queryImages() -> [UIImage?] {

    var iconArray: [UIImage?] = []

    var query: PFQuery = PFQuery(className: "QuestionMaster")
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in

        for object in objects! {

            let imageFiles = object["questionImage"] as! PFFile

            imageFiles.getDataInBackgroundWithBlock({
                (imageData: NSData?, error: NSError?) -> Void in
                if (error == nil) {

                    let image = [UIImage(data: imageData!)]
                    iconArray += image        //adds item to array correctly
                }

                println(iconArray) //prints correct array here

            }) //getDataInBackgroundWithBlock close

            println(iconArray) //does not print correct array here

        } //for-loop close

    }

    return iconArray //returns empty array

}

}

解决方案

As your function states the block is executed in the background (on a asynchronous thread. This means that it will load in the background but will also continue the rest of the function thus returning an empty array.

To fix this you should use a completion handler for your block in background.

func queryImages(onComplete:(images: [UIImage?])-> Void){
    var iconArray: [UIImage?] = []
    var query: PFQuery = PFQuery(className: "QuestionMaster")
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in

    for object in objects! {

        let imageFiles = object["questionImage"] as! PFFile

        let imageData = imageFiles.getData()
        let image = UIImage(data:imageData!)
        iconArray.append(image)

        println(iconArray)
    }
    onComplete(images: iconArray)
    }
}

Although untested above code should work

The Asynchronous data retrieval inside your for loop has been replaced in favor of a synchronous call to ease the project

这篇关于从分析查询块返回UIImage数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆