如何获得所有真实价值

时间:2019-05-24 13:20:34

标签: swift firebase firebase-realtime-database

我有一个像这样的Firebase结构:

"userObjects" : {
    "fP8g5kxrjnBhYTiUxjF6Pdc5xfgP" : {
        "objectsInLists" : {
            "603648351" : {
                "Top" : true
            },
            "097598765" : {
                "Roof" : true
            }
        }
    },

我想在一个objectID下获得所有真实值(097598765是一个objectID)。
所以我想检查097598765是否存在,如果要打印“屋顶”,
到目前为止,我已经来过:

self.ref?.child("userObjects").child(userID!).child("objectsInLists").observeSingleEvent(of: .value, with: { (snapshot) in
    if snapshot.hasChild(self.objectID) {
        print(snapshot.value)
    }
}) 

1 个答案:

答案 0 :(得分:2)

您要查找的是queryOrderedByValue(),因为您要根据查询的子节点的进行过滤:

self.ref?.child("userObjects").child(userID!).child("objectsInLists/097598765")
    .queryOrderedByValue().queryEqual(toValue: true)
    .observeSingleEvent(of: .value, with: { (snapshot) in

        for child in snapshot.children {
           let snap = child as! DataSnapshot
           print(snap.key)
        }
    })