通过创建swift按顺序检索解析对象

时间:2015-12-13 05:06:31

标签: ios swift parse-platform pfquery

代码下方是从parse.com检索。代码工作正常但不按顺序查询我的数据。有人可以解释原因吗?

 func loadData(){
    let findDataParse = PFQuery(className: "JobListing")
    findDataParse.whereKey("user", equalTo: PFUser.currentUser()!)
    findDataParse.orderByAscending("createdAt") 
    findDataParse.findObjectsInBackgroundWithBlock{
        (objects: [PFObject]?, error: NSError?) -> Void in
        if (error == nil) {
            for object in objects! {
               let userImageFile = object["ImageOne"] as! PFFile
                let userImageFile1 = object["ImageTwo"] as! PFFile
                let userImageFile2 = object["ImageThree"] as! PFFile
                userImageFile.getDataInBackgroundWithBlock {
                    (imageData: NSData?, error: NSError?) -> Void in
                    let listingImage1 = UIImage(data:imageData!)
                    userImageFile1.getDataInBackgroundWithBlock {
                        (imageData1: NSData?, error1: NSError?) -> Void in
                        let listingImage2 = UIImage(data:imageData1!)
                        userImageFile2.getDataInBackgroundWithBlock {
                            (imageData2: NSData?, error1: NSError?) -> Void in
                            let listingImage3 = UIImage(data:imageData2!)
                let offers =  Information(objectIDParam: object.objectId!, titleOfProjectParam: object["TitleOfProject"] as! String,  categoryParam: object["Category"] as! String, viewsParam: object["Views"] as! String, image1Param: listingImage1!, image2Param: listingImage2!, image3Param: listingImage3!)

                            self.arrayOfOffers.append(offers)
                            for interests in object["InterestsArray"] as! NSArray{
                                self.arrayOfInterests.append(interests as! String)
                            }
                            self.i++
                            self.tableView.reloadData()
                        }}}}}}}

1 个答案:

答案 0 :(得分:0)

这就是我要做的事情:

  • 创建mutableArray
  • 同时获取所有商品
  • 完成request后,将其object添加到mutableArray并检查mutableArray.count == numberOfRequests
  • 完成所有下载后,对mutableArray
  • 进行排序

这会更快+更容易理解正在发生的事情。