Listen only to additions to a cloud firestore collection?

后端 未结 4 1555
甜味超标
甜味超标 2021-01-04 00:44

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 d

4条回答
  •  清歌不尽
    2021-01-04 01:04

    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.

提交回复
热议问题