【发布时间】:2017-09-01 19:58:36
【问题描述】:
您好,我有一个问题,如果有任何建议或回答,我将不胜感激。
func getUserProfileMDP(){
// 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
print(passwordValue)
self.MDP = passwordValue // prints the right value from database
}){ (error) in
print(error.localizedDescription)
}
print(self.MDP) // prints the value innitialised in class(nope)
}
这是从数据库中获取值的函数。它有效(第一个打印得到正确的值)
@IBAction func register(_ sender: Any) {
print(self.MDP)// prints the value innitialised in class(nope)
getUserProfileMDP()
print(self.MDP) // prints the value innitialised in class(nope)
let MDP = self.MDP
那是我需要密码来比较它。它没有让我得到数据库的值,而是在上面的类中初始化的值:
var MDP = "nope"
祝你有美好的一天
【问题讨论】:
-
因为
getUserProfileMDP()中的.observeSingleEvent是异步的。所以程序控制流并不是你想象的那样。使用回调! -
我是 swift 新手,你能准确地说什么是“回调”吗?
标签: swift3 firebase-realtime-database