[英]Swift firebase createUser function completion block
快速使用Firebase时出现超级怪异的问题。 似乎脚本可以继续执行而无需等待完成块完成。 这就是我的意思:
我有一个功能:
func create_user(email:String, password:String) -> Bool{
var create_suc = false
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if let error = error{
// Error creating new user.
create_suc = false
print("In fail closure: \(create_suc)")
}else{
// Create successfully.
create_suc = true
print("In success closure: \(create_suc)")
}
}
print("Before return: \(create_suc)")
return create_suc
}
以及调用此函数的上下文:
@IBAction func sign_up(_ sender: UIButton) {
let result = create_user(email: acc_email.text!, password: acc_password.text!)
print("Create successfully: \(result)")
我得到的结果很奇怪:
**Before return: false**
**Create successfully: false**
2018-08-26 16:41:55.313457-0700 chat_app[251:9301] TIC Read Status [2:0x0]: 1:57
2018-08-26 16:41:55.313611-0700 chat_app[251:9301] TIC Read Status [2:0x0]: 1:57
2018-08-26 16:41:56.013564-0700 chat_app[251:9300] TIC Read Status [3:0x0]: 1:57
2018-08-26 16:41:56.013721-0700 chat_app[251:9300] TIC Read Status [3:0x0]: 1:57
**In success closure: true**
任务是异步的并不奇怪。
func create_user(email: String, password: String, completion: @escaping(_ res: Bool) -> Void ) {
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if let error = error {
// Error creating new user.
completion(false)
print("In fail closure: \(create_suc)")
} else {
// Create successfully.
completion(true)
}
}
}
create_user(email: acc_email.text!, password: acc_password.text!) { (res) in
print(res)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.