Angular FireStore: applying snapshotChanges() on collection fetches all documents in collection, consume all read operation allowed quickly

≡放荡痞女 提交于 2021-01-29 17:55:03

问题


One of my collection in Firebase's Cloud Firestore has 1500+ records. I am showing that data in a grid in my angular application. The problem is that whenever i refresh the page or as i am developing the application, so on each change/save action, it recompiles and reloads the page, which make 1500 documents read operation. I was assuming that firebase will cache the resultset automatically and snapshotChanges() will just return me the new/updated record but here i am not seeing any cache mechanism. Below is my code i am using to fetch data from Firebase.

getAllProducts(): Observable<Product[]> {

    return this.firestore.collection('Products').snapshotChanges().pipe(map(actions => {

      return actions.map(x => {
        const obj: any = x.payload.doc.data();
        obj.id = x.payload.doc.id;
        return obj as Product;
      });

    }));
  }

I think this is the very common usecase, but most probably i am missing something. Please suggest how can i reduce the number of read operation while maintaining an observable to my collection in CloudFirestore ?

Thanks


回答1:


I was assuming that firebase will cache the resultset automatically and snapshotChanges() will just return me the new/updated record

The persistence layer provided by the Firestore SDK doesn't work like that.

The cache is primarily used to support queries while offline. If you want to specifically use the cache instead of querying the server again, you will need to provide a source option with your query.



来源:https://stackoverflow.com/questions/63271681/angular-firestore-applying-snapshotchanges-on-collection-fetches-all-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!