Firebase 上的好友列表

Friends list on Firebase

到目前为止,在我的 firebase 上,我有类似的东西:

Users
   293840ajldkjfas0d9
       username: Jill
   09283049802930laks
       username: Jack

我基本上只有一个文本字段,当有人点击 "add" 按钮时,通过 firebase 进行搜索以确保用户名存在,然后从那里开始将两者连接为朋友。

我的问题是,如果数据的结构如此,我不明白我将如何对数据进行排序?我可能完全遗漏了一些东西,但有没有办法搜索我现在拥有的东西并说 "Go to my users reference, go to through each UID and pull out the username and see if it matches what the user put in the text field, and if so grab the UID connected to the username"?

我几乎在想我需要一个完全独立的分支?除了我已经拥有的东西之外,我还拥有看起来像

Usernames
   Jack
       09283049802930laks
   Jill
       lasdjf9j09u90320j09

这样我就可以转到我的用户名并检查其中的值 (jack/Jill),然后如果它存在,则将该 uid 从用户名中拉出。

1) 我是否需要这两个分支,或者有没有办法只用我的第一个分支来完成我想做的事情?

2) 我如何检查匹配项?是使用查询还是仅以某种方式检查 Null?

3) 这是否是正确的方法?我在网上的朋友列表上几乎找不到任何信息,所以这只是一个假设。

感谢任何帮助!

Using the below structure, you can check if a user exists with a single query. Here's all the code you need:

这是数据库结构:

Users
293840ajldkjfas0d9
   username: Jill
09283049802930laks
   username: Jack

代码如下:

    var usernameEntered = self.usernameField.text!

    var databaseReferenceQuery = self.ref.child("users").queryOrderedByChild("username").queryEqualToValue(usernameEntered).observeSingleEventOfType(.Value, withBlock: { (snapshot) in

        if ( snapshot.value is NSNull ) {



            // No user :(



        } else {

           // User exists! Do stuff!


        }


        }, withCancelBlock: { (error) in

           // An error occurred
    })

一旦确定用户存在,我可以想出几种方法来获取 uid。关键是要找到uid和username都存放的地方

选项 1:

Create another branch in the users db structure that stores the uid. Set up this branch when the user is signed up.

选项 2:

Store the username as the displayName in the standard auth system and get the uid from there. Set this up when the user signs up.

这取决于你。

Response to comments: You can simply retrieve the key of the user in the table, however this will not allow access to any auth features and will not allow user accounts to be edited in any way.
Examples of situations where this can be useful include updating user info, deleting accounts etc.