admin管理员组文章数量:1323225
I want firestore to fetch data first from cache every time. As per Firestore documentation passing "cache" or "server" options must enable the same. Example below
db.collection("cities").where("capital", "==", true)
.get("cache")
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
However whether i set "cache" or "server", query seems to try network first followed by cache.
I want firestore to fetch data first from cache every time. As per Firestore documentation passing "cache" or "server" options must enable the same. Example below
db.collection("cities").where("capital", "==", true)
.get("cache")
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
However whether i set "cache" or "server", query seems to try network first followed by cache.
Share Improve this question edited Jun 11, 2018 at 13:55 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges asked Jun 11, 2018 at 9:19 Sachin NaikSachin Naik 3552 silver badges15 bronze badges1 Answer
Reset to default 7Since a get()
only gets your a value once, it will always check for the latest value for the data from the server. If this is the first Firestore operation in your app, this may require that it establishes the network connection to the database, which may take some time.
If you quickly want to get the data from the cache, and then later get the modifications from the server, use onSnapshot
:
db.collection("cities").where("capital", "==", true)
.onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
Also see the Firestore documentation on getting realtime updates
To tell get()
to return data from the cache, do:
db.collection("cities").where("capital", "==", true)
.get({ source: 'cache' })
.then(function(querySnapshot) {
...
But note that this means you will never get the updated value from the server.
本文标签: javascriptCache first implementation for get in firestoreStack Overflow
版权声明:本文标题:javascript - Cache first implementation for get in firestore - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742071850a2419172.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论