take(1) on snapshotChanges() : is angular firestore get data from the server on second call even if data didn't change?

Viewed 1276

I'm using Angular Firestore to access a Cloud Firestore database and on almost all my get requests I apply rxjs take(1) operator on snapshotChanges() to not listening to data changes after request is complete.

For instance :

export class myCollectionService {
   constructor(private db: AngularFirestore){}

   getData(){
      return this.db.collection<myCollection>("myCollection")
         .snapshotChanges()
         .pipe(take(1));
   }
}

What happens when I call getData() a second time? Is data gotten from the server even if nothing changes since the previous call or is there some caching mechanism?

1 Answers

Posting this as Community Wiki as it based partially in the comments.

Enabling access to data offline - as explained here - would indeed be a very good idea for you, as you would be dealing with your cached data and you can configure when do you want it to upload with data from the server. Specifically for Angularfire with Firestore you can get more information here - different way of doing, instead of take(1).

Besides that, as explained here, the Firestore SDK doesn't cache automatically, so you need specifically to set that - for example, using the methods informed above. You can also confirm that by using snapshot.metadata.fromCache and verify if it's caching or not.

Related