javascript - 网络应用 : Firestore cache - read changed documents only

标签 javascript firebase google-cloud-firestore

当谈到使用官方 web SDK 的 Firestore 缓存时,它是否优化了读取以便仅当文档在上次读取后发生更改时才向服务器“发送”读取请求? (因此每次尝试不会生成 1 次额外的文档读取)

为了详细说明,请考虑以下场景:

  1. 用户在网络应用上打开他们的个人资料页面
  2. Firestore 向服务器发出请求以获取配置文件文档(1 次读取定价)
  3. 用户离开页面并在一段时间后返回个人资料
  4. 网络应用需要再次访问配置文件

在第 4 步中,Firestore 是否会默认从服务器请求文档,即使它没有更改? (这意味着每次用户导航到个人资料页面时,它将计为 1 次额外阅读)

如果是,是否可以配置 Firestore,使其仅使用缓存的对象并仅在服务器上的对象更改时更新它?如何做到这一点?

最佳答案

默认情况下,Firestore SDK disables local persistence of cached data for web clients (但默认为 Android 和 iOS 启用)。如果要启用持久性,请按照 documentation 中的说明进行操作。

firebase.firestore().enablePersistence()
  .catch(function(err) {
      if (err.code == 'failed-precondition') {
          // Multiple tabs open, persistence can only be enabled
          // in one tab at a a time.
          // ...
      } else if (err.code == 'unimplemented') {
          // The current browser does not support all of the
          // features required to enable persistence
          // ...
      }
  });
// Subsequent queries will use persistence, if it was enabled successfully

如果文档在本地缓存中是最新的,则不会向您收取阅读文档的费用。

关于javascript - 网络应用 : Firestore cache - read changed documents only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54629892/

相关文章:

javascript - 在函数的 for 循环内动态更改元素的内部文本

javascript - 有没有办法为使用 dimplejs 制作的条形图添加水平滚动条?

java - 如何因 Firebase 存储中的文件大小限制规则而出现拒绝上传错误

javascript - 当我调用 google-plus 的登录功能时出现错误

flutter - 如何在Firestore中保存时间戳

javascript - 按下 “A”键时自动播放

javascript - 根据三个模型属性对 Backbone 集合进行排序

java - Android Firebase - 为什么 map 字段中的空值被忽略

javascript - 如何向公众隐藏 HTML/CSS 以及最重要的 app.js(Firebase 代码)?

firebase - 在设定的时间后只运行一次 cron 作业