现在:firebase.firestore().collection("Tweets").onSnapshot(function(querySnapshot) {querySnapshot.forEach(function(doc) {console.log("添加了快照", doc)});});有没有办法只跟踪集合的添加?我想我可以做那个设备端,但没有必要传输我已经查询的所有额外数据..该日志的输出将打印出集合中的每一条推文",无论是否只添加了一条推文例如:初始查询-推文 1 -推文 2-推文 3新推文,已添加推文 4输出:推文 1推文 2推文 3推文 4如果有道理 解决方案 当您使用 onSnapshot() ,您实际上并没有在每次调用时下载整个集合.文档被缓存,当集合再次改变时将被重用.对于导致调用回调的每个更改,您可以通过检查快照中的更改来找出自第一次调用以来哪些文档是新看到的.如何执行此操作的示例是在文档中.有了查询快照,您就可以使用此逻辑来确定哪些文档是新文档:snapshot.docChanges.forEach(function(change) {if (change.type === "已添加") {//change.doc 这里是新建一个新文档}});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 collectionright now:firebase.firestore().collection("Tweets").onSnapshot(function(querySnapshot) { querySnapshot.forEach(function(doc) { console.log("snapshot added ", doc) }); });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 addedEX:Initial Query-Tweet 1-Tweet 2-Tweet 3New Tweet, Tweet 4 is addedOutput:Tweet 1Tweet 2Tweet 3Tweet 4If that makes sense 解决方案 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 }}); 这篇关于只收听 Cloud Firestore 集合的新增内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 06:40