gpt4 book ai didi

ios - 解析查询 orderByDescending 不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 05:38:42 27 4
gpt4 key购买 nike

我有一个检索当前用户和他们在我的 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)")
}
}


}

我如何根据创建日期来排序我的帖子查询?

最佳答案

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

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

为确保查询顺序正确,只执行其中一个查询并打印出结果。

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

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

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

您可以重构您的代码以利用 whereKey:containedIn:

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

关于ios - 解析查询 orderByDescending 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33623232/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com