Swift 这两个代码不是';执行不正常吗?
加载视图控制器时,我基本上希望应用程序:Swift 这两个代码不是';执行不正常吗?,swift,firebase,firebase-authentication,Swift,Firebase,Firebase Authentication,加载视图控制器时,我基本上希望应用程序: 启动匿名登录(使用firebase) 2、打印你好 但是,使用下面的代码,它会执行 你好 然后登录 有什么帮助吗 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. Auth.auth().signInAnonymously{ (user,error) in if l
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
Auth.auth().signInAnonymously{ (user,error) in
if let error = error {
print("Sign in failed: ", error.localizedDescription)
} else {
self.anonId = String(user!.user.uid)
print("Signed in: ",user!.user.uid)
}
}
print("hello")
}
登录Firebase可能需要任何时间,因此它被实现为异步操作。实际上,这意味着依赖于登录结果的任何代码都必须位于回调/完成处理程序中:
Auth.auth().signInAnonymously{ (user,error) in
print("hello")
if let error = error {
print("Sign in failed: ", error.localizedDescription)
} else {
self.anonId = String(user!.user.uid)
print("Signed in: ",user!.user.uid)
}
}
这是现代web API中极为常见的模式,因此最好尽早习惯它。
显然必须是异步的,这意味着它在异步queu上运行操作,然后在完成时调用completionHandler
。print(“Hello”)
虽然在主线程上运行,因此直接执行。请阅读: