简体   繁体   中英

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: "J@j.com"
        name: "Jon"
    -LhkGgXlWOUaZiR3PruD
        email: "R@r.com"
        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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM