[英]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.