Catch
Catch

Reputation: 153

How to get all true values

I have a Firebase structure like this:

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

I would like to get all true values under an objectID (097598765 is one objectID).
So I want check if 097598765 exists, and if it does I want to print "Roof"
This is as far I have come:

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

Upvotes: 0

Views: 95

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599591

What you're looking for is queryOrderedByValue(), since you're looking to filter on the value of the child nodes that you're querying:

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

Upvotes: 2

Related Questions