Firebase中的超级简单查询出错了(Swift)

时间:2019-03-12 21:07:54

标签: swift firebase firebase-realtime-database

我正在尝试根据纪元时间戳做一个简单的有序查询请求:

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;
    };
}

enter image description here

1 个答案:

答案 0 :(得分:0)

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)