[英]How to reduce document reads for Firestore
I am using Firestore (I am new to this) for small web application.我正在将 Firestore(我是新手)用于小型 Web 应用程序。 Currently, each time when I refresh or go to another page, the function retrieves all the documents in the Firestore.目前,每次刷新或转到另一个页面时,该函数都会检索 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) => {
});
});
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根据文档( 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) => {
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.