简体   繁体   English

iOS Swift:从Parse.com检索到的图像没有顺序

[英]iOS Swift: Images retrieved from Parse.com are not in sequence

I have 3 rows in which I have stored images on each row. 我有3行,每行中都存储有图像。

While retrieving the images, I am using "orderBy" to get a standard sequence of objects retrieved. 在检索图像时,我正在使用“ orderBy”来获取检索到的对象的标准序列。 Retrieved strings are in sequence but images are out of sequence. 检索到的字符串是按顺序排列的,但是图像是不按顺序排列的。 Image sequence is changed randomly on each retrieving action. 图像顺序在每次检索操作中都会随机更改。 Can anyone tell me how I can match retrieved strings and image sequence? 谁能告诉我如何匹配检索到的字符串和图像序列? Also, is this the best way to retrieve images with data in parse? 此外,这是检索具有解析数据的图像的最佳方法吗?

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()
        }

This is likely because you're fethcing the images asynchronously within an already asynchronous block, so that when you append the images to the arrays, this process of downloading the images will always take longer than just fetching the name and detail of the PFObject . 这很可能是因为您要在已经异步的块中异步处理图像,因此,当将图像附加到数组时,下载图像的过程将比获取PFObject的名称和详细信息花费更长的PFObject In order to circumvent this, you can try to self.customer.addObject(fName) and self.customerDetails.addObjects(fDetail) inside your if error == nil conditional check. 为了避免这种情况,您可以尝试在if error == nil条件检查中使用self.customer.addObject(fName)self.customerDetails.addObjects(fDetail)

As per @pbush25, I got the code working. 按照@ pbush25,我得到了代码。 Just to help others, I will clearly re write the full working code as below: 为了帮助他人,我将明确重新编写完整的工作代码,如下所示:

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()
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM