我正在將 Firestore(我是新手)用于小型 Web 應用程式。目前,每次重繪 或轉到另一個頁面時,該函式都會檢索 Firestore 中的所有檔案。但它檢索的資料并不經常變化,
有沒有一種方法可以讓我檢索所有資料,而我幾乎不需要讀取檔案?
我目前正在使用這些函式來檢索資料
firebase
.firestore()
.collection("products")
.then((snapshot) => {
snapshot.forEach((docs) => {
});
});
firebase
.firestore()
.collection("products")
.where("prodID", "==", prodID)
.then((snapshot) => {
snapshot.forEach((docs) => {
});
});
uj5u.com熱心網友回復:
這取決于您的應用程式。
但是減少它的一種方法是從快取中檢索它們。
根據檔案(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) => {
});
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329006.html
