【问题标题】:Is it possible to have a completion block on the firestore query?是否可以在 firestore 查询上有一个完成块?
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 谢谢,看到答案,我觉得有点傻。
  • 我认为每个人在开始使用异步代码时都至少犯过一次这种错误。
猜你喜欢
  • 1970-01-01
  • 2016-01-31
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
相关资源
最近更新 更多