从数据库连接两个随机用户 [英] Connect two random users from Database

查看:64
本文介绍了从数据库连接两个随机用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个应用,需要从我的Firebase数据库中匹配两个随机用户.我的问题是我不确定如何将数据库的每个用户连接到随机对: 据我认为,用户可能会按一个按钮来表示他已经准备好,所以他的UID子级(例如搜索")从false变为true.通过接收DataSnapshot,用户可以查看另一个用户是否也在搜索.然后我想到了将两个用户的搜索"状态都设置为false并创建一个新的UniqueID来连接他们.

I’m trying to create an app where I need to match two random users from my Firebase Database. The problem I have is that I‘m not sure how to connect every user of the database to random pairs: As far as I thought the user might press a button to signal that he‘s ready, so a child of his UID like 'searching' turns from false to true. By receiving a DataSnapshot the user can see if another user is searching too. Then I thought of turning the 'searching' state to false for both users and create a new UniqueID to connect them.

但是我认为这样子太短了,会引起一些问题.谁能给我一个关于如何组织搜索过程的提示?

But like that it‘s thought pretty short, I think and would cause some problems. Can anyone give me a hint on how to organize that searching process?

推荐答案

实际上,使用特殊节点(例如searching)并将其设置为truefalse是一个好主意.

Actually using a special node like searching and setting it to true and false will be a good idea.

您可以使用orderByChild()searching节点设置为true的所有用户进行排序,并获取其中任何一个用户的uid.

You can use orderByChild() to order all the users with searching node set to true and get the uid of any of those users.

要使过程随机,您可以打电话给用户,取回给您的ID,然后用基本的随机数随机选择一个.

To make the process random you could make a call to users, take the ids given back to you, and pick one at random with a basic random number.

如果在调用用户后在数组中有10个用户ID,则您希望获取0-9之间的随机数,然后使用userId调用firebase.

If after making a call to users you have 10 user ids in an array you would want to get a random number between 0-9 and then make a call to firebase with the userId.

我认为Firebase对此没有任何内置代码.因此,这将是您可以在应用中实现此功能的最佳选择.

I don't believe Firebase has any built in code for this. So this would be the best thing you can do to achieve this feature in your app.

要给出基本概念,您的数据库结构应如下所示:

To give a basic idea, your database structure should look something like this:

--rootNode
|
|
  -- uid1
| |
|  - searching
|  - other Fields
|
  -- uid2

  .
  .
  .

这篇关于从数据库连接两个随机用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆