问题
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