4

I am using Firestore (I am new to this) for small web application. Currently, each time when I refresh or go to another page, the function retrieves all the documents in the Firestore. But the data that it retrieves does not change often,

Is there a way for me to retrieve all the data that will cost me little to no document reads?

I am currently using these function to retrieve the data

firebase
.firestore()
.collection("products")
.then((snapshot) => {
       snapshot.forEach((docs) => {
       });
});



firebase
.firestore()
.collection("products")
.where("prodID", "==", prodID)
.then((snapshot) => {
       snapshot.forEach((docs) => {
       });
});
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • 1
    I think that this article, [How to drastically reduce the number of reads when no documents are changed in Firestore?](https://medium.com/firebase-tips-tricks/how-to-drastically-reduce-the-number-of-reads-when-no-documents-are-changed-in-firestore-8760e2f25e9e) might help. – Alex Mamo Oct 20 '21 at 06:15

1 Answers1

2

It depends on your app.
But a way to reduce it would be to retrieve them from the cache.
According to the docs (https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/Source) you can do something like

function getData() {
   firebase
   .firestore()
   .collection("products")
   .get({source: "cache"})
   .then((snapshot) => {
         if (!snapshot.exist) return getServerData()
         snapshot.forEach((docs) => {
       });
  });
}

function getServerData() {
   firebase
   .firestore()
   .collection("products")
   .get()
   .then((snapshot) => {
         snapshot.forEach((docs) => {
       });
  });
}   
Antoine Xevlabs
  • 851
  • 1
  • 6
  • 20