Connect two random users from Database

孤者浪人 提交于 2020-01-17 01:36:25

问题


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?


回答1:


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

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

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.

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.

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

  .
  .
  .


来源:https://stackoverflow.com/questions/53467067/connect-two-random-users-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!