【问题标题】:grab the current users first name cloud firebase swift获取当前用户的名字 cloud firebase swift
【发布时间】:2019-10-20 04:33:26
【问题描述】:

我有一个存储用户的 Firebase 身份验证,但它还在 Cloud Firestore 数据库中构建了一个用户集合。我可以获取用户的名字,但我遇到的问题是它始终是添加的最后一个用户。

这是我在 swift 中的功能

func welcomeName() {

    let db = Firestore.firestore()
    if let userId = Auth.auth().currentUser?.uid {

    var userName = db.collection("users").getDocuments() { (snapshot, error) in
        if let error = error {
            print("Error getting documents: \(error)")
    } else {
        //do something
            for document in snapshot!.documents {
        var welcomeName = document["firstname"] as! String
        self.welcomeLabel.text = "Hey, \(welcomeName) welcome!"
                }
            }
        }
    }
}

在 Firebase 云中,我的用户是这样存储的

开始收集是“用户”

添加文档是autoID

那么集合就是

名字 “简”

姓氏 “母狗”

uid "IKEPa1lt1JX8gXxGkP4FAulmmZC2"

有什么想法吗?

【问题讨论】:

  • Option1) 存储您的用户数据,其中 documentID 是用户 uid 而不是 autoID。选项 2)您还可以根据当前结构对特定用户 uid 执行查询,但查询比直接读取文档要“重”得多,因此选项 1 是可行的方法。

标签: swift firebase google-cloud-firestore cloud uid


【解决方案1】:

当您循环浏览所有用户文档的列表时,它将在标签上显示最后一个用户的firstName。您可能希望如下所示显示第一个用户,

if let firstUserDoc = snapshot?.documents.first {
   var welcomeName = firstUserDoc["firstname"] as! String
   self.welcomeLabel.text = "Hey, \(welcomeName) welcome!"
}

如果列表中的uiduserId 相同,则可能是当前用户,

if let currentUserDoc = snapshot?.documents.first(where: { ($0["uid"] as? String) == userId }) {
   var welcomeName = currentUserDoc["firstname"] as! String
   self.welcomeLabel.text = "Hey, \(welcomeName) welcome!"
}

【讨论】:

  • 我想这就是正在发生的事情。但是如果用户不是第一个或最后一个怎么办?如何通过登录用户获取用户?
  • @BlueMoose 在下有提及。必须有一些密钥才能找到文档属于哪个用户。
  • 你是对的。我想我删除了一个括号。现在它的工作!感谢您的帮助!
  • @BlueMoose 等一下,伙计们。虽然这是一个答案,但从长远来看并不是一个好的答案。随着您的应用程序的增长,您可能拥有数千甚至数十万用户。使用此解决方案,整个数据库中的每个用户都必须连同他们的所有用户数据一起加载到应用程序中,然后遍历这些用户,直到找到正确的用户.这可能需要很长时间,还可能使设备不堪重负并崩溃。更好的解决方案是将用户数据存储在文档中,将用户的uid作为documentID,然后您可以直接读取您想要的用户文档。
  • 明白。但是,您的答案不是 Firebase 提供的用于查询特定用户的内容。答案会遍历每个已经加载并寻找特定用户的用户。 Firebase 有一个谨慎的查询,如果 uid 已知,它将返回一个且仅一个文档或用户。需要明确的是,答案没有错,但随着应用程序的扩展,这不是一个好的解决方案。
【解决方案2】:

你的用户集合应该是这样的

users (collection)
   uid_0 (document that's the users uid)
      first_name: "William"
   uid_1
      first_name: "Henry"
   uid_2

然后,当用户进行身份验证时,您将知道他们的 uid,因此您可以直接从 Firestore 获取信息而无需查询。

func presentWelcomeMessage() {
    if let userId = Auth.auth().currentUser?.uid {
        let collectionRef = self.db.collection("users")
        let thisUserDoc = collectionRef.document(userId)
        thisUserDoc.getDocument(completion: { document, error in
            if let err = error {
                print(err.localizedDescription)
                return
            }
            if let doc = document {
                let welcomeName = doc.get("first_name") ?? "No Name"
                print("Hey, \(welcomeName) welcome!")
            }
        })
    }
}

如果用户 William 登录,这将打印到控制台

嗨,欢迎威廉!

【讨论】:

    猜你喜欢
    • 2020-03-03
    • 2018-01-28
    • 2019-08-15
    • 2018-03-17
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多