iOS Swift:从Parse.com检索的图像不是按顺序排列的

时间:2015-07-06 16:35:41

标签: ios swift parse-platform

我有3行,其中我已经在每行存储了图像。

在检索图像时,我使用“orderBy”来获取检索到的标准对象序列。检索到的字符串是按顺序排列的,但图像不按顺序排列。每次检索操作都会随机更改图像序列。谁能告诉我如何匹配检索到的字符串和图像序列?另外,这是使用解析数据检索图像的最佳方法吗?

var customer = NSMutableArray()
var customerDetails = NSMutableArray()
var profilePic : Array<UIImage> = []

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

                var fName = object["customerName"]! as! String
                var fImage = object["customerImage"] as! PFFile
                var fDetail = object["customerDetail"]! as! String

                fImage.getDataInBackgroundWithBlock({
                    (imageData: NSData?, error: NSError?) -> Void in
                    if (error == nil) {
                        var cImages = UIImage(data:imageData!)
                        println(cImages)
                        self.profilePic.append(cImages!)
                    }

                })//getDataInBackgroundWithBlock - end
                self.customer.addObject(fName)
                self.customerDetails.addObject(fDetail)
            }//for - end

            self.tableView.reloadData()
        }

2 个答案:

答案 0 :(得分:1)

这很可能是因为您在已经异步的块中异步处理图像,因此当您将图像附加到数组时,下载图像的过程总是比获取图像的名称和细节花费更长的时间。 PFObject。为了避免这种情况,您可以在self.customer.addObject(fName)条件检查中尝试self.customerDetails.addObjects(fDetail)if error == nil

答案 1 :(得分:0)

根据@ pbush25,我得到了代码。只是为了帮助别人,我将清楚地写下完整的工作代码,如下所示:

var customer = NSMutableArray()
var customerDetails = NSMutableArray()
var profilePic : Array<UIImage> = []

 var query:PFQuery = PFQuery(className: "Images")
    query.orderByAscending("createdAt") // This may or may not work. I still didn't make it work each time I run the code.
    query.findObjectsInBackgroundWithBlock{(objects:[AnyObject]?, error: NSError?) -> Void in
        if(error == nil){
            for object in objects as! [PFObject!] {

            fImage.getDataInBackgroundWithBlock({
                (imageData: NSData?, error: NSError?) -> Void in
                if (error == nil) {
                    var cImages = UIImage(data:imageData!)
                    var fName = object["customerName"]! as! String
                    var fDetail = object["customerDetail"]! as! String

                    self.customer.addObject(fName)
                    self.customerDetails.addObject(fDetail)
                    self.profilePic.append(cImages!)
                }

            })//getDataInBackgroundWithBlock - end

        }//for - end

        self.tableView.reloadData()
    }