swift - 是否可以在 firestore 查询上有一个完成 block ?

标签 swift firebase-authentication google-cloud-firestore

我目前有一个 Firestore 数据库引用,它查询数据以尝试找到具有特定用户名的用户。获得用户后,我想尝试登录。但是,我注意到查询用户的代码块在调用后立即返回。有什么方法可以添加完成 block 或至少停止程序直到完成查询。

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 数据库图像的链接 /image/hI1iY.png

最佳答案

您需要将 signIn 放在 getDocuments 处理程序中,可能在 for 循环之后?

关于swift - 是否可以在 firestore 查询上有一个完成 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47487678/

相关文章:

ios - 如何正确地 "clean"从 Swift 中的 Superview 中删除的 UIView? - IOS

SWIFT - 为什么我想要发帖时总是出错?

firebase - 如何让 Firestore 规则与服务帐户一起使用

java - 有没有办法从android中的firestore中的数组获取数据

firebase - 如何在 Firebase Firestore 中创建 Multi-Tenancy SaaS 应用权限

swift - 通过类注入(inject)引用 Observable

java - 不将自定义java对象保存在android studio的firebase中

javascript - 通过 Firebase auth() 管理其他用户帐户

Firebase 用户枚举攻击

swift - 从一个类调用另一个类的函数,Xcode 错误