database - 使用 Swift 使用唯一 UID 作为键在 Firebase 中查询用户

标签 database swift firebase

我正在尝试通过用户注册时自动保存的小写字符串名称查询用户。问题是我正在为使用他们的 uid 的用户创建 key 。现在我很难让其他用户找到他们的 friend ,因为我要撤回的只是第一个关键“用户”。这是我的火力基地信息...... Firebase data of user with keys: caseUsername, displayName, follows, and image

这是我的代码...

let userRef = self.ref.childByAppendingPath("users")
userRef.queryOrderedByChild("caseUsername")
       .queryStartingAtValue(self.searchTXTFLD.text?.lowercaseString, childKey: "caseUsername")
       .observeEventType(.Value, withBlock: { (snapshot) -> Void in
           print(snapshot.key)

请帮我找到使用 self.searchTXTFLD.text?.lowercaseString 的用户。它应该等于用户子 caseUsername

最佳答案

感谢@Frank van Puffelen。答案更多地阐明了 Firebase 数据应该如何构建。我没有完全使用你的答案,但这是我所拥有的和有效的。

  @IBAction func searchFriendsACTN(sender: AnyObject)
        {
            let queryRef = self.ref.childByAppendingPath("users")
            queryRef.queryOrderedByChild("caseUsername")
                .queryEqualToValue(self.searchTXTFLD.text?.lowercaseString)
                .queryLimitedToFirst(1)
                .observeEventType(.Value, withBlock: { snapshot in
                    if snapshot.exists()
                    {
                        var newIds = [String]()
                        var newNames = [String]()
                        var newCsnames = [String]()
                        var newPics = [String]()
                        for items in snapshot.value as! NSDictionary
                        {
                            let ids = items.key
                            let names = items.value["displayName"] as! String
                            let csnames = items.value["caseUsername"] as! String
                            let pics = items.value["image"] as! NSString

                            newIds.append(ids as! String)
                            newNames.append(names)
                            newCsnames.append(csnames)
                            newPics.append(pics as String)
                        }
                        self.caseUserNameArray = newCsnames
                        self.usersID = newIds
                        self.usernameArray = newNames
                        self.photos = newPics
                        self.friendsTBLVW.reloadData()
                    }
                })
        }

关于database - 使用 Swift 使用唯一 UID 作为键在 Firebase 中查询用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36107167/

相关文章:

swift - 奇怪的 NSButton 创建

MySQL错误1005?

android - Activity 和服务如何同时写入数据库?

ios - 使用 FIRAuth 实现创建用户时遇到问题

Swift 协议(protocol)错误 : 'weak' cannot be applied to non-class type

nested - Firebase 嵌套查询

node.js - 用户登录时 Firebase 创建条目

android - 在 Android 上检索用户数据 Firebase

java - 在ListView中显示JsonArray

javascript - Node.js 不从浏览器执行 DB 函数