Retrieving value from database

前端 未结 1 1988
日久生厌
日久生厌 2021-01-23 14:42

Hi I have a problem and would be grateful to any advice or answer.

   func getUserProfileMDP(){

    // set attributes to textField
    var ref: DatabaseReferenc         


        
相关标签:
1条回答
  • 2021-01-23 15:12

    Given your last comment, I'd say that you're almost there, but here's an example. I did not fix the other parts of your code, I only added the completion handler in the method signature, and passed the password value to the handler, to show you how this works. The handler must be called inside the async closure.

    func getUserProfileMDP(completion: @escaping (String)->()) {
        // set attributes to textField
        var ref: DatabaseReference!
        ref = Database.database().reference()
        let user = Auth.auth().currentUser
        print(user!.uid)
        ref.child("users").child((user?.uid)!).observeSingleEvent(of: .value, with: { (snapshot) in
            // Get user value
            guard let value = snapshot.value as? [String: String] else { return }
            print(value)
            let passwordValue = value["password"]!as! String
    
            completion(passwordValue)
    
        }){ (error) in
            print(error.localizedDescription)
        }
    }
    

    And you call it like that:

    getUserProfileMDP() { pass in
        print(pass)
        self.MDP = pass
    }
    
    0 讨论(0)
提交回复
热议问题