I have a query that retrieve current user and their friend list post on my feed page, i tried to order the post by it's creation date or the latest date. Here is the result that I got from my code when I print the "createAt":
The result is very random.
And here is my code
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)")
}
}
}
How can I order my post query by it's created at date?
The postQuery
is wrapped in a loop to iterate through all of friends in friendsArray
. Since the query is executed in the background, the loop will continue while the query is being executed and still waiting for a response.
This will result in multiple queries and responses being executed simultaneously, and my guess is that you are viewing the print statements from multiple queries which is giving the appearance of being out of order.
To make sure that the query is ordered properly, execute just one of the queries and print out the results.
Lastly, you should avoid executing a loop of queries at all costs because it will not be scalable at all.
For example, instead of wrapping the query in a loop and using
postQuery.whereKey("createdBy", equalTo: friend)
you may be able to refactor your code to utilize whereKey:containedIn:
postQuery.whereKey("createdBy", containedIn: friendsArray)