1

First time asking a question here, so sorry if I do it wrong.

Anyways. I'm using Firebase Database to store "Results" in my Quiz app. When data is store it looks like this

Results

 -LjQ34gs7QoL1GMufiMsaddclose
 Score: xx
 UserName: xx

 -LjQ3NeCoDGob8wnhstH
 Score: xx
 UserName: xx

I would like to access score and username from it and display it in a HighScore tableview. Problem is - I can get the "Results" node, but because of the id of the results (ie LjQ34gs7QoL1GMufiMsaddclose) I don't know how to access the score and username.

I got the data snapshot​, but not sure how to "bypass" the id to get to score and username.

Hope I made it at least a bit clear, what the problem is.

let ref = Database.database().reference().child("Results")

ref.observe(.value) { (DataSnapshot) in
    print(DataSnapshot.value as Any)
}
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56

1 Answers1

0

You current code gets you a single snapshot with the results of all users. You'll need to loop over the child snapshots to get the result of each user, and then look up their specific properties with childSnapshot(byName:):

let ref = Database.database().reference().child("Results")

ref.observe(.value) { (snapshot) in
    for case let userSnapshot as DataSnapshot in snapshot.children {
        print(userSnapshot.childSnapshot(forPath: "Score").value)
    }

}

Also see:

And probably some more from this list.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807