如何减少 Firestore 的文档读取

How to reduce document reads for Firestore

我正在为小型 Web 应用程序使用 Firestore(我是新手)。目前,每次我刷新或转到另一个页面时,该函数都会检索 Firestore 中的所有文档。但是它检索的数据并不经常更改,

有没有一种方法可以让我检索所有的数据,而这些数据几乎不需要我阅读文档?

我目前正在使用这些函数来检索数据

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



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

这取决于您的应用程序。
但是减少它的一种方法是从缓存中检索它们。
根据文档 (https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/Source) 你可以做类似

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) => {
       });
  });
}