解析查询orderByDescending不起作用

时间:2015-11-10 05:04:20

标签: ios iphone swift parse-platform pfquery

我有一个查询在我的Feed页面上检索当前用户及其朋友列表帖子,我试图按照创建日期或最新日期订购帖子。这是我在打印“createAt”时从代码中获得的结果:

enter image description here

结果非常随机。

这是我的代码

for friend in friendsArray {
            let postQuery = PFQuery(className:"Post")
            postQuery.whereKey("createdBy", equalTo: friend)
            postQuery.includeKey("Song")
            postQuery.includeKey("createdBy")
            postQuery.orderByDescending("createdAt")
            postQuery.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]?, error: NSError?) -> Void in

                if error == nil {
                    // The find succeeded.
                    //print("Successfully retrieved \(objects!.count) scores.")
                    // Do something with the found objects
                    if let objects = objects as? [PFObject] {
                        for object in objects {

                            let songObject : PFObject = object["Song"] as! PFObject
                            let songTitle: AnyObject? = songObject.objectForKey("songTitle")
                            let songArtist: AnyObject? = songObject.objectForKey("songArtist")
                            let albumCover: AnyObject? = songObject.objectForKey("albumCover")
                            let previewUrl: AnyObject? = songObject.objectForKey("previewUrl")
                            let trackviewUrl : AnyObject? = songObject.objectForKey("trackViewUrl")

                            let timeAgo = object.createdAt
                            let postCaption: AnyObject? = object.objectForKey("postCaption")
                            let postId: AnyObject? = object.objectId
                            let recentTotalComment: AnyObject? = object.objectForKey("recentTotalComment")
                            let recentTotalLike: AnyObject? = object.objectForKey("recentTotalLike")

                            let posterObject = object["createdBy"] as! PFObject
                            let posterUser = object["createdBy"] as! PFUser
                            let posterImage: AnyObject? = posterObject.objectForKey("profilePicture")
                            let posterUsername: AnyObject? = posterObject.objectForKey("username")


                            self.postDictionary = ["title" : songTitle!, "artist" : songArtist!, "previewUrl" : previewUrl! , "caption" : postCaption!, "timeAgo" : self.timeAgoSinceDate(timeAgo!, numericDates: true), "cover" : albumCover!, "postId" : postId!, "posterImage" : posterImage!, "posterUsername" : posterUsername!, "likeCount" : recentTotalLike!, "commentCount" : recentTotalComment! , "otherUser" : posterUser , "trackViewUrl" : trackviewUrl!]

                            self.postArray.addObject(self.postDictionary!)

                            self.likeArray.addObject(recentTotalLike as! String)
                            self.commentArray.addObject(recentTotalComment as! String)

                            print("created at : \(object.createdAt)")

                            self.tbl.reloadData()                                 
                            self.tbl.setContentOffset(CGPointMake(0, self.verticalContentOffset), animated: true)
                            self.refreshControl.endRefreshing()
                        }
                    }
                } else {
                    // Log details of the failure
                    print("Error: \(error!) \(error!.userInfo)")
                }
            }


        }

如何根据日期创建的帖子查询订购?

1 个答案:

答案 0 :(得分:0)

postQuery被包裹在一个循环中,以遍历friendsArray中的所有朋友。由于查询是在后台执行的,因此循环将在执行查询时继续,并且仍在等待响应。

这将导致多个查询和响应同时执行,我的猜测是您正在查看来自多个查询的打印语句,这些查询会出现乱序。

要确保正确排序查询,请仅执行其中一个查询并打印出结果。

最后,您应该不惜一切代价避免执行查询循环,因为它根本不可扩展。

例如,不是将查询包装在循环中并使用

postQuery.whereKey("createdBy", equalTo: friend)

您可以重构代码以使用whereKey:containedIn:

postQuery.whereKey("createdBy", containedIn: friendsArray)