Rishabh
Rishabh

Reputation: 23

Retrieve current user from firebase realtime database (Swift 4/5)

I want to get my current user's name to display on a navigation bar.

This is using the newest version of Swift (5) and the newest version of Firebase for iOS (6.3.0).

This is the database I have. CURRENT USER is "Rishabh"

rishabhmessage-cd077
  users
    -LhfNh3w6jYW4t71y2BM
        email: "[email protected]"
        name: "Jon"
    -LhkGgXlWOUaZiR3PruD
        email: "[email protected]"
        name: "Rishabh"
let uid = Auth.auth().currentUser?.uid

Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value,  with: {
      (snapshot) in
       print("1")
       if let dictionary = snapshot.value as? [String: AnyObject] {
            print("2")
            self.navigationItem.title = dictionary["name"] as? String
       }
}, withCancel: nil)

This is supposed to update the navigation bar's title FOR THE CURRENT USER whose name is "Rishabh". I added some print statements for debugging. print("1") displays in the console while print("2") does not.

Upvotes: 0

Views: 277

Answers (1)

Loren Rogers
Loren Rogers

Reputation: 359

Either the snapshot.value is nil OR it's not a type: [String:AnyObject]. What I would do is break on the if let dictionary line and then look at what snapshot.value is

Upvotes: 1

Related Questions