gpt4 book ai didi

ios - 如何正确实现Parse查询来获取有序数据

转载 作者:行者123 更新时间:2023-11-30 11:36:29 25 4
gpt4 key购买 nike

我有下面的代码来从两个查询中构建一个查询。

问题是结果未按 updateAt 排序。

可能是什么问题?

let ownerQuery = PFQuery(className: "requests")
ownerQuery.whereKey("owner", equalTo: forUser)
ownerQuery.whereKey("stage", equalTo: "initiated")

let requestQuery = PFQuery(className: "requests")
requestQuery.whereKey("requested_by", equalTo: requestedBy)
requestQuery.whereKey("stage", equalTo: "accepted")

// fetch all request of current user as requestor or lender/seller
let query = PFQuery.orQuery(withSubqueries: [ownerQuery, requestQuery])

query.includeKey("requested_by")
query.includeKey("owner")
query.order(byDescending: "updatedAt")

最佳答案

问题原来是我处理检索到的数据的代码。解析返回排序结果。

我有下面的代码,其中排序受到影响。这似乎是因为 getDetails() 以异步方式返回。

所以我在返回之前添加了数组排序。我一次检索 20 行,因此我猜对每个数组进行排序应该不会很昂贵。

我还在 GitHub 上发过帖子。等待确认。

query.findObjectsInBackground { (results, error) in
if error == nil {
for result in results! {

group.enter()

let stage = result["stage"] as! String
let requestedBy = result["requested_by"] as! PFUser

DBHelper.getDetails(result["requested_id"] as! String, callbackFunction: { (requestedItem) in
let owner = result["owner"] as! PFUser
let request = Request()

request.requestId = result.objectId
request.item = requestedItem

request.owner = owner
request.lastUpdatedDate = result.updatedAt
requestsFound.append(request)

group.leave()
})
}
group.notify(queue: DispatchQueue.global(qos: .background), execute: {
DispatchQueue.main.async {
requestsFound.sort {(request1:Request, request2:Request) -> Bool in
request1.lastUpdatedDate! < request2.lastUpdatedDate!
}
callbackFunction(requestsFound,nil)
}
})

} else {
// encountered error from Parse
DispatchQueue.main.async {

callbackFunction(requestsFound,error! as NSError) // to represent error at backend server

}

}


} //end of findObjectsInBackground

关于ios - 如何正确实现Parse查询来获取有序数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49709660/

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