Firestore 离线缓存 [英] Firestore offline cache

查看:27
本文介绍了Firestore 离线缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Android 应用程序,该应用程序必须离线工作数周,但可以在远程数据库上线时立即同步.

我的问题是 Firestore 是一个不错的选择吗?Firestore 会将其离线缓存保留多长时间?

解决方案

Firestore 可以配置为为此类断开连接/离线使用保留数据.我建议您阅读启用离线持久性部分在文档中,其中包含启用此功能的示例:

<块引用>

FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()

 .setPersistenceEnabled(true).建造();db.setFirestoreSettings(设置);

这个持久化实际上在Android和iOS上是默认开启的,所以不需要上面的调用.

无论您是否连接,与数据库交互的 Android 代码都是相同的,因为 SDK 的工作原理相同.如果您想检测数据是否来自缓存(因此可能是陈旧的),请阅读部分 听文档中的离线数据.

缓存中的数据在一定时间后不会过期.数据从缓存中删除的唯一两个原因:

  1. 数据已从服务器中删除,在这种情况下,客户端会将其从磁盘缓存中删除.
  2. 客户端需要清除其磁盘缓存,以便为更新的数据腾出空间.

I'm building an Android application which has to work offline for weeks, but can sync immediately with a remote DB as it goes online.

My question is can Firestore be a good option for this? How long does Firestore keep its offline cache?

解决方案

Firestore can be configured to persist data for such disconnected/offline usage. I recommend that you read the enable offline persistence section in the docs, which contains this sample of enabling this feature:

FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()

        .setPersistenceEnabled(true)
        .build();
db.setFirestoreSettings(settings);

This persistence is actually enabled by default on Android and iOS, so the call above is not needed.

Your Android code that interact with the database will be the same whether you're connected or not, since the SDK simply works the same. If you want to detect whether data is coming from the cache (and thus potentially stale), read the section Listen to offline data in the docs.

The data in the cache does not expire after a certain amount of time. The only two reasons data is removed from the cache:

  1. The data has been removed from the server, in which case the client will remove it from the disk cache.
  2. The client needs to purge its disk cache to make space for more recent data.

这篇关于Firestore 离线缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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