将firestore嵌套在react-native中的onSnapshot侦听器上? [英] Nesting firestore onSnapshot listeners in react-native?

查看:82
本文介绍了将firestore嵌套在react-native中的onSnapshot侦听器上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用执行以下操作:

I am trying to do something like this for my app:

snapshotListeners() {
  firestore()
    .collectionGroup()
    .where()
    .onSnapshot({
      error: //Handle error
      next: firstSnapshot => {
        firstSnapshot.docsChanges().forEach(change => {
          //Data retrieved and used in the below query to get more data
          firestore()
            .collection()
            .where() //using the data retrived from the above query here.
            .onSnapshot({
              error: //Handle error
              next: secondSnapshot => {
                secondSnapshot.docsChanges().forEach(change =>{
                  //More Data from second query
                })
              }
            })
        })
      }
    })
}

第二个查询取决于从第一个查询中检索到的数据,我希望在各自文档中的两个查询中侦听更改.

The second query is dependent on the data retrieved from the first one, and I wish to listen for changes across both queries in their respective documents.

例如,组功能:这是我的数据库结构:

For example, Group Functionality: This is my db structure:

Groups(top-level collection)
    |_groupId(documents) - Members(subcollection) 
    |_groupId             |     |_uid(document)
                          |     |_uid
                          |
                          |__groupdata(Fields like title, description, etc)

因此,我将使用第一个侦听器来侦听Members子集合中特定用户文档的更改,并使用第二个侦听器来检索组(有关群组的数据),他是实时的,因此当他从群组中添加/删除时,列表/前端会像WhatsApp一样自动更新.

So I will use the first listener to listen for changes to the documents of that particular user in the Members sub-collection and the second listener for retrieving the groups(data about the groups) he is in realtime so that when he is added/removed from the group, the list/frontend automatically updates just like WhatsApp.

或者即使更改组数据(如标题,说明等),这些更改也会在前端听到并更新.

Or even if the group data is changed like the title, description, etc. those changes are heard and updated in the front-end.

在此用例中建议使用这种快照侦听器嵌套吗?还是有另一种方法?

Is this nesting of snapshot listeners recommended for this use case or there is another way?

非常感谢您的帮助.

推荐答案

像这样的嵌套侦听器是很正常的事情,如果以合理的规模完成,则无需担心.合理是有点主观和依赖的,但可以说:适合移动应用程序的单个屏幕的东西可能是一个合理的上限.

Nesting listeners like this is quite normal and, when done at a reasonable scale, not a concern. What is reasonable is a bit subjective and dependent, but let's say: something that fits on a single screen in a mobile app is probably a reasonable cap.

请记住,您需要自己管理监听器.因此,请保留已添加的嵌套侦听器的列表,并仅在外部侦听器触发时添加新侦听器(并删除过时的侦听器).

Keep in mind that you'll need to manage the listener yourself though. So keep a list of nested listeners you've added, and only add the new ones when the outer listener fires (and remove the outdated ones).

如果您希望进一步扩展,可以将每个成员(嵌套文档)中的相关数据复制到组(父文档)中.

If you want something that scale further, you can duplicate the relevant data from each member (the nested documents) into the group (the parent document).

作为第三种选择,您可以仅为特定组的成员创建一个单独的子集合,以便可以使用单个快照侦听器来侦听所有这些对象.

As a third alternative, you can create a separate subcollection for just the members of a particular group, so that you can listen to all of them with a single snapshot listener.

这篇关于将firestore嵌套在react-native中的onSnapshot侦听器上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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