admin管理员组文章数量:1178527
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
right 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 added
EX: Initial Query
-Tweet 1
-Tweet 2
-Tweet 3
New Tweet, Tweet 4 is added
Output:
Tweet 1
Tweet 2
Tweet 3
Tweet 4
If that makes sense
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
right 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 added
EX: Initial Query
-Tweet 1
-Tweet 2
-Tweet 3
New Tweet, Tweet 4 is added
Output:
Tweet 1
Tweet 2
Tweet 3
Tweet 4
If that makes sense
Share Improve this question edited Feb 24, 2018 at 19:13 billybob2 asked Feb 24, 2018 at 16:43 billybob2billybob2 7011 gold badge10 silver badges18 bronze badges 2- We need more information here. What is this.DataRef? A collection reference? A query? Please be specific. Also, what is the contents of the database you're working with? What's the log output with those specific contents? – Doug Stevenson Commented Feb 24, 2018 at 18:16
- 1 sorry, I just made an edit – billybob2 Commented Feb 24, 2018 at 19:12
4 Answers
Reset to default 23When 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 seems that the onSnapshot
is only for listening an entire document,which means it would return all of the fields in a document that you're listening. So you are not able to only retrieve the changed field.
But you can do different things according to different types of change:
xxxx.onSnapshot(function(querySnapshot){
querySnapshot.docChanges.forEach(function(change){
if(change.type=="added"){//first time it will be triggered
}else if(change.type == "modified"){//modified
}else if(change.type == "removed"){//removed
}
})
})
Hope this helps
The documents are cached and hence they are not completly downloaded. But if the complete data is bigger than cache size, it might be ree downloaded. The best strategy is to enable cache storage to disk and then using limitToLast(1).
firebase.firestore().collection("Tweets")
.orderBy("key1")
.limitToLast(1)
.onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log("snapshot added ", doc)
});
});
in this key1 could be the tweet id.
This will leverage automatic single property index and will only fetch the last record. if persistence is enabled, this fetch will be from the cache.
Multiple answers above have talked about how to figure about if this data is new or stale.
You can get the new document that caused this callback
for change in changes:
if change.type.name == 'ADDED':
print(f'New document: {change.document.id}')
本文标签: javascriptListen only to additions to a cloud firestore collectionStack Overflow
版权声明:本文标题:javascript - Listen only to additions to a cloud firestore collection? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738099473a2063447.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论