我无法按顺序从解析中检索图像

时间:2019-07-10 20:46:57

标签: swift xcode parsing pfquery

我被困住了,我以为我有答案,但是有25%的时间我从信息中解析信息的顺序不正确。 我有一个名为countsSorted的变量,其关键字是查询(字符串)的结果,countsSorted的值是该查询的计数(一个Int)。

我的目标是在countSorted ....中显示来自String的ObjectID的图像,并将其与链接的计数配对。

在各个论坛上,我意识到事情需要排序,因此在我检查代码时已经做到了。 我最初的想法是捕获ObjectIds而不是直接获取PFFile,我将应用索引并希望使用这些索引来实现我的目标,我读过字典可能有帮助,但是我无法使[indexPath.row]正常工作

结构会起作用吗? 我可以在这篇文章中包括我所有的失败,但我要的是一些建议,并希望能解释为什么会发生这种情况以及下一步要解决的问题。

let usersQRCodes = PFQuery(className:"UserQRCodes")
            usersQRCodes.whereKey("userName", equalTo:     PFUser.current()!)
            usersQRCodes.findObjectsInBackground { (results, error) in
                if error != nil {
                    print(error!)
                } else if let results = results {
                    for object in results {
                        if let array = object["info"] as? String {
                            self.infoTakenFromUsersParse.append(array)
                            self.infoTakenFromUsersParse.sort(by: {$0 < $1})
                           }
                    }
                     self.infoTakenFromUsersParse.forEach {self.counts[$0, default: 0] += 1 }
                    let countsSorted = self.counts.sorted(by: {$0.value > $1.value})
                    print("This is countsSorted \(countsSorted)")

                    for (key, value) in countsSorted {
                        self.whatINeed.append(value)
                        self.numberOfStampees.append(key)

                   print("This is What I Need \(self.whatINeed)")
                   print("This is object in self.counts \(key)")
                    let getObjectId = PFQuery(className:"QRCodes")
                        getObjectId.whereKey("info", equalTo: key)
                        getObjectId.findObjectsInBackground { (results, error) in
                            if error != nil {
                                print(error!)
                            } else if let results = results {
                                for object in results {
                                 self.objectIds.append(object.objectId!)
                                    if let userImage = object["RewardCard"] as? PFFileObject {
                                    self.stampeeCardsPfile.append(userImage)
                                        self.myTableView.reloadData()
                                    }
                                }

                            }
                           self.myTableView.reloadData()
                        }
                    }
                }
            }
        }


Console: 

This is countsSorted [(key: "Nel\'s Nails", value: 3), (key: "Coffee Shop", value: 2), (key: "T-Bone", value: 1)]
This is What I Need [3]
This is object in self.counts Nel's Nails
This is What I Need [3, 2]
This is object in self.counts Coffee Shop
This is What I Need [3, 2, 1]
This is object in self.counts T-Bone
This is the ObjectsID Array ["u7mRy8bTUT"]
This is the ObjectsID Array ["u7mRy8bTUT", "xSQVuLHcj0"]
This is the ObjectsID Array ["u7mRy8bTUT", "xSQVuLHcj0", "SuXofzyoBD"]

//这是单元格

self.stampeeCardsPfile[indexPath.row].getDataInBackground { (data, error) in
            if let error = error {
                print(error.localizedDescription)
            }else{

                if let imageData = data {

                    if let imageToDisplay = UIImage(data: imageData) {


                        self.stampeeCards.append(imageToDisplay)

                        cell.qrImage.image = imageToDisplay
                        cell.qrLabel.text = String(self.whatINeed[indexPath.row])


                    }
                }
            }

        }
        cell.stamps.image = self.collectedStampees[self.whatINeed[indexPath.row]]

        return cell

    }

一切正常,直到objectID数组应为[“ u7mRy8bTUT”,“ SuXofzyoBD”,“ xSQVuLHcj0”]为止,但它是随机的。 图像按此顺序下载,因此我希望通过按顺序排列这些objectId的顺序来解决这些问题。

0 个答案:

没有答案