解析查询 orderByDescending 不起作用
Parse query orderByDescending not working
我有一个检索当前用户及其好友列表 post 的查询,我尝试按创建日期或最新日期排序 post。这是我打印 "createAt":
时从我的代码中得到的结果
结果很随机。
这是我的代码
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)")
}
}
}
如何按创建日期来排序我的 post 查询?
postQuery
被包裹在一个循环中以遍历 friendsArray
中的所有朋友。由于查询是在后台执行的,因此在执行查询并仍在等待响应时循环将继续。
这将导致同时执行多个查询和响应,我的猜测是您正在查看来自多个查询的打印语句,这看起来像是乱序的。
为确保查询顺序正确,只执行其中一个查询并打印出结果。
最后,您应该不惜一切代价避免执行查询循环,因为它根本不可扩展。
例如,不是将查询包装在一个循环中并使用
postQuery.whereKey("createdBy", equalTo: friend)
您可以重构代码以利用 whereKey:containedIn:
postQuery.whereKey("createdBy", containedIn: friendsArray)
我有一个检索当前用户及其好友列表 post 的查询,我尝试按创建日期或最新日期排序 post。这是我打印 "createAt":
时从我的代码中得到的结果结果很随机。
这是我的代码
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)")
}
}
}
如何按创建日期来排序我的 post 查询?
postQuery
被包裹在一个循环中以遍历 friendsArray
中的所有朋友。由于查询是在后台执行的,因此在执行查询并仍在等待响应时循环将继续。
这将导致同时执行多个查询和响应,我的猜测是您正在查看来自多个查询的打印语句,这看起来像是乱序的。
为确保查询顺序正确,只执行其中一个查询并打印出结果。
最后,您应该不惜一切代价避免执行查询循环,因为它根本不可扩展。
例如,不是将查询包装在一个循环中并使用
postQuery.whereKey("createdBy", equalTo: friend)
您可以重构代码以利用 whereKey:containedIn:
postQuery.whereKey("createdBy", containedIn: friendsArray)