嗨我有一个问题,并会感谢任何意见或答案。从数据库检索值
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"
有一个愉快的一天
因为'.observeSingleEvent'在'getUserProfileMDP()'是异步的。所以程序控制流程不是你想象的那样。使用回拨! – Moritz
我是新来的swift你能准确地知道什么是“回调”? –