ios - 从 Parse 检索多个图像的更好方法

标签 ios swift parse-platform

菜鸟问题在这里,我知道下面的代码是非常错误的,但它可以检索我需要的 3 个图像。但是,我想知道一种从 Parse 检索多个图像的更好方法。

任何帮助将不胜感激!

func retrieveImage() {

    var query = PFQuery(className: "Items")
    query.orderByDescending("createdAt")
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            let imageObjects = objects as! [PFObject]
            for (index, object) in enumerate(imageObjects) {

                let thumbnail1 = object["image1"] as! PFFile
                let thumbnail2 = object["image2"] as! PFFile
                let thumbnail3 = object["image3"] as! PFFile

                thumbnail1.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
                    if error == nil {
                        if let image = UIImage(data: imageData!) {
                            self.itemImages[index] = image
                    }
                }
                thumbnail2.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
                    if error == nil {
                        if let image = UIImage(data: imageData!) {
                            self.itemImages2[index] = image
                            }
                        }
                    }
                thumbnail3.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
                    if error == nil {
                        if let image = UIImage(data: imageData!) {
                            self.itemImages3[index] = image
                        }
                    }
                }
            }
        }
    }
}

}

最佳答案

首先是这个想法...我们想要执行任意长的异步任务列表,收集它们的结果,并在完成或错误时收到通知。我们通过参数化任务来实现这一点(在本例中,要获取其内容的 PFFile 就是参数),并将这些参数用作“待办事项列表”。

递归函数完成这项工作,选取列表中的第一项,执行异步任务,然后使用列表的其余部分调用自身。空的待办事项列表意味着我们已经完成了。

我尝试翻译答案I referred to here进入 swift(逐行学习语言)....

func load(pfFiles: Array<PFFile>, var filling: Dictionary<PFFile, UIImage>,  completion: (success: Bool) -> Void) {
    completion(success: true)
    var count = pfFiles.count
    if (count == 0) {
        return completion(success: true)
    }
    var file = pfFiles[0]
    var remainder = Array(pfFiles[1..<count])

    file.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
        if error == nil {
            if let image = UIImage(data: imageData!) {
                filling[file.name] = image
                self.load(remainder, filling: filling, completion: completion)
            }
        } else {
            completion(success: false)
        }
    }
}

鉴于这是我的第一次尝试,如果它有效,我会感到有点震惊和高兴,但算法是合理的,并且 swift 编译并似乎符合我概述的想法。以下是如何调用它...

var pfFiles: Array<PFFile>
for (index, object) in enumerate(imageObjects) {
    pfFiles.append(object["image1"])
    pfFiles.append(object["image2"])
    pfFiles.append(object["image3"])
}
var filling: Dictionary<String, UIImage>
// call the function here
// in the completion assign filling to property
// anytime after, when you have a PFFile like someObject["image2"]
// you use its name to look it up the UIImage in the results dictionary

请告诉我最后一点是否足够清楚。正如您所看到的,我对快速翻译失去了动力,并求助于伪代码。

关于ios - 从 Parse 检索多个图像的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32058737/

相关文章:

javascript - 使用evaluateJavaScript(..)调用javascript函数,以读取应用程序包中的文本文件

c# - Xamarin iOS 导航到设置

ios - .pch 文件中的#define 不起作用

android - 使用 Volley 发布 JsonObject

iOS - 解析 - 更新其他 PFUser

ios - 如何保存图像

ios - 禁用 UIPickerView 中的特定行值

ios - UIDocumentInteractionController 总是失败

ios - 向 UILabel 添加手势

iphone - 用户授权应用程序后解析 api 删除 'you already authorized this app'