Return UIImage Array From Parse Query Block

前端 未结 1 1397
孤独总比滥情好
孤独总比滥情好 2021-01-28 06:38

I cannot get a [UIImage?] return from this function. The getDataInBackgroundWithBlock won\'t let me set a return value other than Void in. However, tha

相关标签:
1条回答
  • 2021-01-28 07:25

    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

    0 讨论(0)
提交回复
热议问题