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

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

问题描述

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

将目标部署到本机应用程序(iOS,Android),电子应用程序(Windows和Mac),PWA.

  1. firestore本地缓存在混合应用程序中是否持久存在? Firestore缓存使用哪种类型的存储? 会是类似于localstorage之类的东西吗,会在存储空间不足的情况下不时由android/iOS删除.

  2. 我正在使用以下代码进行测试,并且确实启用了perPersistence,脱机模式工作正常. 但似乎它统计了每次应用启动时读取的所有文档. 例如,我有100个文档.

a.应用首次启动时,由于它将所有数据同步到本地缓存,因此应计为100读.

b.在我第二次启动该应用程序时,假设没有文档被更新,它不应该算读权吗?

c.因为从我的监视来看,每次启动应用程序时,读取计数都会增加.

d.可能会像没有文档更新一样,但是我的代码强制从服务器获取数据,然后消耗读取计数吗?

谢谢.

 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 };
      }))
    );
  } 

解决方案

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

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

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

2. a-您说过:应用首次启动时,将所有数据同步到本地缓存时,它应该算作100读",但是如上所述,Firestore只会加载您应用正在使用的数据,因此大多数情况下,正在查看总数据的一部分(100个文档).
b-关于您声称不进行文档修改的主张,无论如何,您的应用程序都会同步数据,这已经意味着需要进行验证.在此文档中指出: 当设备重新联机时,Cloud Firestore客户端库将自动管理联机和脱机数据访问并同步本地数据.
c-此声明是正确的,并且指向我之前指出的句子.

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

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

  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.

  2. 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.

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

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

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

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?

Thanks.

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. 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. 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天全站免登陆