异步任务完成后加载PageViewController

时间:2015-02-07 18:24:59

标签: objective-c swift asynchronous

异步调用完成后,我无法加载PageViewController。我正在考虑使用NSNotification,但不确定什么是最好的方法。

Async func to fetch images

func fetchArrayImages() {
    var query = PFQuery(className: "FoodPhoto")
    query.orderByDescending("Votes")
    query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
        if(error == nil){
            let imageObjects = objects as [PFObject]
            for object in objects {
                let photoUploaded = object["PhotoUploaded"] as PFFile
                photoUploaded.getDataInBackgroundWithBlock({
                    (imageData: NSData!, error: NSError!) -> Void in
                    if (error == nil) {
                        let image = UIImage(data:imageData)
                        //image object implementation
                        self.photosUploadedArray.append(image!)
                    }
                })
            }
        }
        else{
            println("Error in retrieving \(error)")
        }
    })
}

在异步下载图像后调用Func

这会加载UIPageViewController

func loadPhotosFromArray() {
    var array = photosUploadedArray
    view1 = PhotoCollevtionView(outerFrame: self.view.frame, photoArray: array, currentNumber: 0)
    self.view.addSubview(view1!)
}

1 个答案:

答案 0 :(得分:0)

您可以检查上传的图片是否与imageObjects数组中的最后一张图片相同,在这种情况下,您可以像这样调用loadPhotosFromArray()代码:

self.photosUploadedArray.append(image!)
if ( imageObjects.last.isEqual(image!)) { //Use this code
    loadPhotosFromArray()
 }