我正在尝试根据纪元时间戳做一个简单的有序查询请求:
guard let user = Auth.auth().currentUser?.uid else {return}
let queryRef = Database.database().reference().child("ordersBackup").child(user).queryOrdered(byChild: "pickupCompleteAfter").queryLimited(toFirst: 1)
queryRef.observeSingleEvent(of: .childAdded, with: { (snapshot) in
print(snapshot.value)
})
结构体:
{
"UserId" : {
"UniqueId1" : {
"pickupCompleteAfter" : 1568314979000,
"name" : "Jeff"
},
"UniqueId2" : {
"pickupCompleteAfter" : 1557687779000,
"name" : "Stig"
},
"UniqueId3" : {
"pickupCompleteAfter" : "1578855779000",
"name" : "Ali"
}
}
}
它应该返回UniqueId2。但是,我总是检索与结构相同的顺序,即UniqueId1,UniqueId2,UniqueId3。它根本没有考虑时间戳。我究竟做错了什么?
输出:
LQ8nHi
{
orderInfo = {
deliveryCompleteAfter = 1552917600000;
deliveryCompleteBefore = 1552924800000;
pickupCompleteAfter = 156249280000;
pickupCompleteBefore = 1552492800000;
};
}
YQxeKv
{
orderInfo = {
deliveryCompleteAfter = 1552917600000;
deliveryCompleteBefore = 1552924800000;
pickupCompleteAfter = 1557687779000;
pickupCompleteBefore = 1552492800000;
};
}
ibIPO9
{
orderInfo = {
deliveryCompleteAfter = 1553090400000;
deliveryCompleteBefore = 1553097600000;
pickupCompleteAfter = 1578855779000;
pickupCompleteBefore = 1552665600000;
};
}
最佳答案
pickupCompleteAfter
嵌套在每个子代的orderInfo
节点下。您当前没有告诉数据库该完整路径,因此它仅检查每个子项下的pickupCompleteAfter
。
要在orderInfo/pickupCompleteAfter
上订购/过滤,请使用:
let query = Database.database().reference().child("ordersBackup").child(user)
.queryOrdered(byChild: "orderInfo/pickupCompleteAfter").queryLimited(toFirst: 1)
query.observeSingleEvent(of: .childAdded, with: { (snapshot) in
print(snapshot.value)