Akbar
Akbar

Reputation: 15

I cant see values from firebase swift

Screen Shot 2019-04-01 at 4.45.08 PMenter image description hereI want to get the value from firebase that is stored I want to print it into a text label,

func fetchData(){
    var ref: DatabaseReference!
    ref = Database.database().reference()
    guard  let currentUid = Auth.auth().currentUser?.uid else {return}
    Database.database().reference().child("users").child(currentUid).observeSingleEvent(of: .value, with: { (snapshot) in
        guard let dictionary = snapshot.value as? Dictionary<String , AnyObject> else {return}
        let uid = snapshot.value
        let user = User(uid:uid as! String, dictionary: dictionary)
        self.user = user
    }) { (error) in
        print(error.localizedDescription)
    }
}

This is my another code to set the text label

func fetchData(){
    var ref: DatabaseReference!

    ref = Database.database().reference()
    guard  let currentUid = Auth.auth().currentUser?.uid else {return}
    Database.database().reference().child("users").child(currentUid).observeSingleEvent(of: .value, with: { (snapshot) in
        guard let dictionary = snapshot.value as? Dictionary<String , AnyObject> else {return}
        let uid = snapshot.value
        let user = User(uid:uid as! String, dictionary: dictionary)
        self.userNameLabel.text = user.username
        self.user = user

        print(snapshot)
    }) { (error) in
        print(error.localizedDescription)
    }
}

extra code to understand what I am doing

// user info stored

  let userID = Auth.auth().currentUser?.uid
        let userData = ["userName": userName,
            "userAge ": userAge] as [String? : Any]
         let values = [userID: userData]

        let ref = Database.database().reference()
        ref.child("users").childByAutoId().setValue(values)



}

Upvotes: 0

Views: 172

Answers (2)

Akbar
Akbar

Reputation: 15

I have found the answer I wasn't setting the user stored info in the firebase the right way.

          let userID = Auth.auth().currentUser?.uid

          let userData = ["userName": userName,

         "userAge ": userAge] as [String? : Any]



        let ref = Database.database().reference()


       ref.child("users/\(userID ?? "")").setValue(userData)



}

//ref.child("users/(userID??"")").setValue(userData) // this was the error

Upvotes: 0

Wilfried Josset
Wilfried Josset

Reputation: 1256

  1. I think you should use callbacks to communicate asynchronously with your controller.
  2. In your example you get the uid by using "snpashot.value" but you should take the key of your dictionary instead.
  3. Here is a similar example :

    ///Function that returns as callback the user stored in Firebase with a certain id.
    func getUserFor(id: String, callback: @escaping (Bool, User?) -> Void) {
    
        //Get the user in Firebase "user" collection with the specific id.
        ref.child(Constants.userKey).child(id).observeSingleEvent(of: .value, with: { (userSnapshot) in
    
            guard let userDictionnary = userSnapshot.value as? [String:Any] else { return callback(false, nil) }
    
            let userId = userSnapshot.key
            let userEmail = userDictionnary[Constants.emailKey] as? String
    
            let user = User(id: userId, email: userEmail)
    
            return callback(true, user)
        }) { (error) in
            print(error.localizedDescription)
            return callback(false, nil)
        }
    }
    

Upvotes: 2

Related Questions