如何在标签中加载Firebase中的数据?

时间:2018-03-12 15:52:49

标签: swift firebase label

问题是标签不会使用“Nick”孩子中来自Firebase的文字更改其文字。

这就是我的代码的样子:

ref = Database.database().reference()
refHandle = ref.observe(DataEventType.value, with: { (snapshot) in
    let dataDict = snapshot.value as! [String: AnyObject]
    print(dataDict)
})

let userID: String = (Auth.auth().currentUser?.uid)!
ref.child("Users").child(userID).child("Nick").observeSingleEvent(of: .value, with: { 
    snapshot in 
    print(snapshot)
    let m = snapshot.value as? String
    self.label.text = m
})

这就是我的Firebase数据的样子:

enter image description here

2 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您正在使用Firebase库中的observeSingleEvent方法,而是使用observe方法:

ref.child("Users").child(userID).observe(.value, with: { snapshot in
    if let snapshotDict = snapshot as? [String:Any] {
        if let nick = snapshotDic["Nick"] as? String {
            self.label.text = m
        }
    }
})

答案 1 :(得分:0)

试试这个:

        ref = Database.database().reference().child("Users").child(userID)
        ref.observeSingleEvent(of: .value, with: { snapshot in
            if let snap = snapshot.value as? [String : AnyObject] {
               if let result = snap["Nick"] as? String {
                  self.label.text = result
               }else
                  print("nick not exist")
               }
            }else{
               print("maybe USER UID not exist in database ")
            }
         })