如何从Firebase中撤消此列表的顺序?我希望最后一项成为第一项。我尝试使用queryOrdered
,但这并没有什么区别。我列出了一个我从Firebase检索的数据结构示例:
"-KhPRPQEOiVzPW7G2iQI" : {
"amount" : "20",
"coordinate" : {
"lat" : 28.10388759332169,
"long" : -81.46038228976457
},
"creationTime" : "2017-04-11 00:37:29 +0000",
"creator" : "6MAK7nkzoBT6NzIxj7DW3564cf72",
"description" : "A ride ",
"distanceRadius" : 1609.34,
"expireTime" : "2017-04-11 03:37:00 +0000",
"paymentNonce" : "51df2b01-9e97-0fce-14d5-fc8fdf98603c",
"provider" : "PSEFUZdAAxRH14oYnyzE7yMpcil1",
"status" : "accepted"
},
代码:
func retrieveRquestFromFir(){
var searchType:String!
switch self.messageType {
case messageTypeEnum.creator:
searchType = "creator"
break
case messageTypeEnum.provider:
searchType = "provider"
break
}
//TODO: Fix here
let path = "rquest/frontEnd/posts/userCreatedPost"
self.childRef(path).queryOrdered(byChild: "creationTime") .observe(.childAdded, with: {snapshot in
if let snapDict = snapshot.value as? NSDictionary {
if let postId = snapDict[searchType] as? String {
if postId == self.currentUserId()! {
let rquestId = snapshot.key
let status = snapDict["status"] as! String
//check to see if message is pending or accepted in order to show messages
if status == "pending" || status == "accepted" {
let description = snapDict["description"] as! String
let amount = snapDict["amount"] as! String
let creator = snapDict["creator"] as! String
var provider:String?
// calculate expire time here
var expireTime:String!
let dateString = snapDict["expireTime"] as! String
var date:Date!
if status == "pending" {
date = Date(string: dateString, format: "yyyy-MM-dd HH:mm:ss Z")!
if date < Date() {
return
}
}else if status == "accepted" {
expireTime = snapDict["expireTime"] as! String
date = Date(string: dateString, format: "yyyy-MM-dd HH:mm:ss Z")!
}
if let providerUser = snapDict["provider"] as? String {
provider = providerUser
}
let array = [
"expireTime":dateString,
"amount":amount,
"description":description,
"rquestId":rquestId,
"status": status,
"provider":provider as Any,
"creator": creator
] as NSDictionary
//check if date is more than 5 days if it is more than dont Added
let dateAmount = Date.daysBetween(start: date, end: Date())
// let StringDate = newDate?.string(withFormat: "yyyy-MM-dd HH:mm:ss Z")
if dateAmount <= 5 {
DispatchQueue.main.async {
self.RquestInfoArray.append(array)
self.tableView.reloadData()
}
}
}
}
}else {
DispatchQueue.main.async {
self.RquestInfoArray.append(array)
self.tableView.reloadData()
}
}
}
})
}
答案 0 :(得分:1)
正如@Frank所说,你需要在客户端对结果进行排序。
您可以使用类似的
对数组进行排序let sorted = self.RquestInfoArray.sorted(by: { (r1: RequestInfoType, r2: RequestInfoType) -> Bool in
return r1.dateAmount > r2.dateAmount
})
但是如果你真的想要反转数组,可能更简单的只是在开始时添加新元素而不是结束
self.RquestInfoArray.insert(contentsOf: array, at: 0)
答案 1 :(得分:1)
我正在使用的一个技巧是向我的对象添加一个字段,该字段将时间戳存储为反转值。当我保留对象时,我计算的该字段的值为 JSON.parse(this.question_set_id)['id']
现在,您可以使用默认的Firebase reversedDateCreated = timeStamp * -1
方法来按orderBy
字段进行排序和分页