Firestore + Ionic Angularfire 读取计数 &缓存持久化 [英] Firestore + Ionic Angularfire Read Count & Cache Persistent

查看:30
本文介绍了Firestore + Ionic Angularfire 读取计数 &缓存持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Ionic 4 + Firestore 为应用提供离线和实时同步功能.

I'd like to use Ionic 4 + Firestore to provide app with offline and live sync capability.

目标部署到原生应用(iOS、Android)、电子应用(Windows 和 Mac)、PWA.

Target deploy to Native App (iOS, Android), electron App (Windows and Mac), PWA.

  1. firestore 本地缓存是否在混合应用中持久化?使用什么类型的存储 Firestore 缓存?会不会是localstorage之类的,会不时被android/iOS删除/存储空间不足.

  1. Is firestore local cache persistent in hybrid app? What type of storage firestore cache using? Will it be something like localstorage, which will be delete by android / iOS from time to time / while low storage.

我正在使用以下代码进行测试并启用了持久性,离线模式工作正常.但似乎它计算了每次应用程序启动时读取的所有文档.例如,我有 100 个文档.

I'm testing with below code and did enablePersistence, offline mode is working just fine. But it seem that the the it count all documents read per app launch. Example, I'm having 100 documents.

一个.当应用首次启动时,它应该计为 100 次读取,因为它将所有数据同步到本地缓存.

a. While app first launch, it should count as 100 read as it sync all data to local cache.

B.当我第二次启动应用程序时,假设没有更新任何文档,它不应该算作任何阅读,对吗?

b. While 2nd time I launch the app, assume, no document was updated, it shouldn't count any read right?

c.因为根据我的监控,每次启动应用程序时读取计数都会增加.

c. Because from my monitoring, the read count increase every time I launch the app.

d.有没有可能,没有文档被更新,但我的代码强制从服务器获取数据然后它消耗了读取计数?

d. Will it be any possible like, no document was updated, but my code force to fetch data from server then it consume the read count?

谢谢.

getChatMessages(groupId) {
    return this.db.collection(`groups/${groupId}/messages`, ref => ref.orderBy('createdAt')).snapshotChanges().pipe(
      map(actions => actions.map(a => {
        const data = a.payload.doc.data();
        const id = a.payload.doc.id;
        return { id, ...data };
      }))
    );
  }

推荐答案

我猜你在谈论离线 数据,我认为您的问题可以通过文档得到解答.

I guess you are talking about the offline data, I think your question can be answered with the docs.

1.1.Firestore 本地缓存在混合应用程序中是否持久?它应该是因为该功能随您应该在混合应用程序中使用的客户端库一起提供.来自本文档:此功能缓存云的副本您的应用正在使用的 Firestore 数据,因此您的应用可以在设备离线时访问这些数据.是的,它是本地存储.

1.1. Is firestore local cache persistent in hybrid app? It should since the functionality comes with the client libraries that you should be using into your hybrid app. From this doc:This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. YES, it’s local storage.

1.2.使用什么类型的存储 Firestore 缓存?来自本文档:Cloud Firestore 是一个云托管的 NoSQL 数据库.您将数据存储在包含映射到值的字段的文档中.

2. a - 你说:当应用首次启动时,它应该算作 100 次读取,因为它将所有数据同步到本地缓存",但如上所述,Firestore 只会加载你的应用正在使用的数据,所以大部分是你的正在查看总数据的子集(100 个文档).
b- 关于您关于假设没有修改文档的声明,无论如何您的应用程序将同步数据,这已经意味着验证请求.在本文档 中指出:Cloud Firestore 客户端库会自动管理在线和离线数据访问,并在设备重新上线时同步本地数据.
c- 这个说法是正确的,指向我之前提到的那句话.

1.2. What type of storage firestore cache using? From this doc: Cloud Firestore is a cloud-hosted NoSQL database. You store data in documents, which contain fields mapping to values.

2. a - You stated: "While app first launch, it should count as 100 read as it sync all data to local cache", but as mentioned above, Firestore will only load the data that your app is using actively, so mostly your are seeing a subset of your total data (100 documents).
b- Regarding your claim about assuming no document modification, anyway your app will sync the data, which already means a validating request. At this doc it’s stated: The Cloud Firestore client library automatically manages online and offline data access and synchronizes local data when the device is back online.
c- This claim is correct and points to the sentence I pointed before.

这篇关于Firestore + Ionic Angularfire 读取计数 &缓存持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆