为什么 auth() 函数响应迟了?当 status 返回时,它总是返回函数开头定义的值。有没有办法用函数调用 auth() createUser?
func register(email: String, password: String) -> Dictionary<String, Any> {
var status = ["stat": false] as Dictionary<String, Any>
let handle = Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
if error != nil {
status["stat"] = false
status["statement"] = error!.localizedDescription as String
} else {
status["stat"] = true
}
}
return status
}
The handle function returns before the end of the register function.
最佳答案
createUser
是一个异步方法,您可以延迟返回值直到createUser
完成,但这不是处理异步方法的最佳方式。
如果您为原始方法提供一个完成 block ,例如:
func register(email: String, password: String, completion: @escaping(([String: Any]) -> Void)) {
var status: [String: Any] = ["stat": false]
let handle = Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
if error != nil {
status["stat"] = false
status["statement"] = error!.localizedDescription as String
} else {
status["stat"] = true
}
completion(status)
}
}
然后在任何其他地方,您可以确定 createUser
已完成其过程:
register(email: "email", password: "password") { status in
// Now it's ready.
print(status)
}
关于ios - 为什么 Swift Firebase auth() 响应晚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59658589/