zander
zander

Reputation: 27

Read one instance of Firebase DB - Swift

I have a Firebase Real Time Database that looks like this:

I am trying to read the value attribute of Balance, but I am not getting any response when I run this code in Swift:

 var userID = Auth.auth().currentUser?.uid
 var ref = Database.database().reference()

 func observeDB() {
 ref.child(userID!).child("Balance").observe(.childAdded, with: { snapshot in
        let userDict = snapshot.value as! [String: Any]
        print(userDict["value"] as! String)
    })
}

Is there another way that I can solve this problem? Thanks!

Upvotes: 0

Views: 50

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

This seems simpler to me:

ref.child(userID!).child("Balance/value").observe(.value, with: { snapshot in
    print(snapshot.value)
})

Differences with your code:

  1. This listens directly to the value child of Balance.
  2. This uses a listener on the .value event.

Both seems more direct translations of what you're trying to accomplish.

Upvotes: 1

Related Questions