【发布时间】:2017-11-25 15:40:32
【问题描述】:
我目前有一个 Firestore 数据库引用,它查询数据以尝试查找具有特定用户名的用户。获得用户后,我想尝试登录。但是,我注意到查询用户的代码块会在调用后立即返回。有没有办法添加一个完成块或至少停止程序直到它完成查询。
u.name = name
global.db.collection("users").whereField("username", isEqualTo: u.name).getDocuments(completion: { (snap, error) in
if error != nil {
print(error?.localizedDescription as Any)
return
}
for doc in (snap?.documents)! {
u.email = doc.data()["email"] as! String
}
})
Auth.auth().signIn(withEmail: u.email, password: password, completion: { (user, error) in
if error != nil {
print(error?.localizedDescription as Any)
return
}
print("Succesfully Logged In")
self.toListSelector(user: u)
})
这是我的 Firestore 数据库图像的链接 https://i.stack.imgur.com/hI1iY.png
【问题讨论】:
-
在你的代码中已经有是一个完成块:它是你循环
snap?.documents的地方。正如 Josh 回答的那样,您需要将Auth.auth().signIn代码移入回调块中。
标签: swift firebase-authentication google-cloud-firestore