Julio Didier Maciel
Julio Didier Maciel

Reputation: 157

Simple query in Firebase

I'm new with Firebase in Swift and I see that there are more people in the same boat. My idea here is to retrieve the score of each player. Firebase db is like this:

enter image description here

Which is on the root reference (dbRef = FIRDatabase.database().reference()) My code is doing this:

dbRef.queryOrdered(byChild: "score").observeSingleEvent(of: .value) { (snap: FIRDataSnapshot) in
        for child in snap.children{
            print(child)
        }
    }

Which is generating these messages in terminal:

Snap (zfUNewjOCsQ5VCtSLFXSXLSFXtJ3) {
    addedByUser = "[email protected]";
    score = 100;
}
Snap (yfJqO65qCwTTlgzrcw1ovmPgZ6l2) {
    addedByUser = "[email protected]";
    score = 1000;
}

How to get on the score so the message printed in terminal be like:

100

1000

thanks in advance

Upvotes: 0

Views: 102

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599946

You'd use childSnapshotForPath to get to the score:

dbRef.queryOrdered(byChild: "score").observeSingleEvent(of: .value) { (snap: FIRDataSnapshot) in
    for child in snap.children{
        print((child as AnyObject).childSnapshot(forPath: "score").value)
    }
}

See https://firebase.google.com/docs/reference/ios/firebasedatabase/api/reference/Classes/FIRDataSnapshot#-childsnapshotforpath

Upvotes: 1

Related Questions