在Firebase中随机配对用户 [英] Randomly pairing users in Firebase

查看:80
本文介绍了在Firebase中随机配对用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flutter和Firebase开发应用程序,我必须解决一个听起来像这样的问题:

I'm developing an application using Flutter and Firebase and I have to solve a problem that sounds like this:


  • 每个用户(随时)可以将自己置于等待列表

  • Firebase必须通过创建(随机)对用户
  • each user (at any time) can put himself in a waiting list
  • Firebase has to handle the waiting list by creating (randomly) pairs of users

我的想法是在Cloud Firestore中创建一个等待列表集合,其中每个用户都是文档,并且具有 cloud功能通过在集合中搜索另一个用户(文档),删除两个文档(即从等待列表中删除用户)并在另一个表示该对的集合中创建另一个文档,对文档创建做出反应。
主要问题是设计(如果可能)事务,以确保同一云功能的两个或多个执行不重叠。
有什么建议吗?或任何其他方式来解决这个问题?
谢谢!

My idea is to have a collection in the Cloud Firestore for the waiting list in which each user is a document and a cloud function that reacts to the document creation by searching for an other user (document) in the collection, deleting both documents (i.e. removing the users from the waiting list) and creating another document in another collection that represents the pair. The main issue is to design (if possible) a transaction in order to guarantee that two or more executions of the same cloud function do not overlap. Any suggestion? Or any other way to solve this problem? Thanks!

推荐答案

您可以将所有用户放入具有唯一ID的单个文档中和以下数据:

You could put all the users in a single document with Unique ID and data like following:

然后,您可以使用 Cloud Function ,它将执行以下操作:

Then you could use a Cloud Function and that will execute following things:


  1. 检索 Array 在特定文档中称为 waiting
    会将新帐户用户ID 添加到该数组。

  1. Retrieving the Array called waiting from a specific document. It will add new account user id to that array.

如果数组至少有2个元素。它将从2中添加名为 paired 的新的
数组地图映射 waiting 列表中的元素和
将值添加到firestore中。它将调用现有的 paired
array 并添加新的2个元素并将其保存到 Firestore 。还要更新
用户文档中的 paired_with 字段。

If the array has got at least 2 elements. It will add a new array of maps named paired from the 2 elements from the waiting list and add the value to firestore. It will call the existing paired array and add the new 2 elements and save it to Firestore. Also update the paired_with field in users doc.

它将从等待中删除2个元素list更新一个空的
列表。

It will remove the 2 elements from waiting list update an empty list.

这篇关于在Firebase中随机配对用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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