gpt4 book ai didi

ios - 想要在 firebase 身份验证完成后调用方法

转载 作者:行者123 更新时间:2023-11-30 11:22:38 26 4
gpt4 key购买 nike

   func signUpPressed(){
guard let email = signupview.emailTextField.textField.text, let password = signupview.passwordTextField1.textField.text, let name = signupview.nameTextField.textField.text else{
return
}
Auth.auth().createUser(withEmail: email, password: password, completion: {(User, error) in

if error != nil{
print(error)
return
}
let ref = Database.database().reference()
let uid = User?.user.uid!
let userReference = ref.child("Users").child((uid)!)
let values = ["name": name, "email": email]
userReference.updateChildValues(values, withCompletionBlock: {(err, ref) in

if err != nil{
print(err)
return
}
print("User saved to firebase")
})
})
getUserInfo()
}

我希望由此实现的是能够在 firebase 创建用户后存储所有用户信息。问题是 getUserInfo 方法在 print("User saving to firebase") 之前执行。我还了解到 Firebase 是异步的,这意味着调用总是立即返回,而不会阻塞代码等待结果。稍后,只要准备好,结果就会出来。现在我想要做的是在结果“进来”之后调用一个方法。我怎样才能做到这一点?

最佳答案

异步不会阻塞主线程,也不会阻止方法

getUserInfo()   

在响应返回之前运行,因此您需要将其插入到 updateChildValues 的完成 block 中,如下所示

func signUpPressed(){
guard let email = signupview.emailTextField.textField.text, let password = signupview.passwordTextField1.textField.text, let name = signupview.nameTextField.textField.text else{
return
}
Auth.auth().createUser(withEmail: email, password: password, completion: {(User, error) in

if error != nil{
print(error)
return
}
let ref = Database.database().reference()
let uid = User?.user.uid!
let userReference = ref.child("Users").child((uid)!)
let values = ["name": name, "email": email]
userReference.updateChildValues(values, withCompletionBlock: {(err, ref) in

if err != nil{
print(err)
return
}

getUserInfo()

print("User saved to firebase")
})
})

}

关于ios - 想要在 firebase 身份验证完成后调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51119194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com