只收听云火灾收藏品的附加信息? [英] Listen only to additions to a cloud firestore collection?

查看:95
本文介绍了只收听云火灾收藏品的附加信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当我尝试在firestore中的Collection上使用实时监听器时,每次将新Document添加到集合中时,逻辑将重新运行,我将下载集合中已有的所有内容

I've noticed, when I try to use a realtime listener on a Collection in firestore, each time a new Document is added to the collection, the logic will be rerun, and I will download everything already in the collection

现在:

firebase.firestore().collection("Tweets").onSnapshot(function(querySnapshot) {
      querySnapshot.forEach(function(doc) {
        console.log("snapshot added ", doc)
      });
    });

有没有办法只追踪到集合的ADDITIONS?我想我可以做那个设备端,但是没有必要转移我已经查询过的所有额外数据..

Is there a way to track only ADDITIONS to the collection? I guess I could do that device side, but theres no need to transfer all the additional data I have already queryed for..

该日志的输出将打印出来集合中的推特,无论是否只添加一个

Output of that log would print out every single "tweet" in the collection, regardless of only one being added

EX:
初始查询

EX: Initial Query

-Tweet 1

-Tweet 2

-Tweet 3

新推文,推文4已添加

输出:

Tweet 1

Tweet 2

Tweet 3

Tweet 4

如果有意义

推荐答案

当您使用 onSnapshot()集合上,您实际上并未下载整个集合每次调用。这些文档被缓存,并在集合再次更改时重复使用。

When you use onSnapshot() on a collection, you're not actually downloading the entire collection on each invocation. The documents are cached and will be reused when the collection changes again.

对于导致调用回调的每个更改,您都可以找到新的文档。通过检查快照中的更改来进行第一次调用。有关如何执行此操作的示例,请参阅文档中的。有了查询快照,您可以使用此逻辑来确定哪些文档是新的:

For each change that causes your callback to be invoked, you can find out which documents are new seen since the first invocation by checking the changes within the snapshot. An example of how to do this is in the documentation. With a query snapshot in hand, you can use this logic to determine which documents are new:

snapshot.docChanges.forEach(function(change) {
    if (change.type === "added") {
        // change.doc here is new a new document
    }
});

这篇关于只收听云火灾收藏品的附加信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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