Firebase:setPersistenceEnabled 和 keepSynced 有什么区别? [英] Firebase : What is the difference between setPersistenceEnabled and keepSynced?

查看:22
本文介绍了Firebase:setPersistenceEnabled 和 keepSynced 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直以为在我使用以下聊天对话的所有数据的整个过程中都可以随时离线使用.不知何故不是,所有节点都从服务器加载.

I thought the whole time when I used the following all data for chat conversation will be available offline at any time. Which somehow isn't and all nodes are loaded from the server.

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

然后,根据 DOCS :

持久性行为:通过启用持久性,我们在在线时同步的任何数据都将持久化到磁盘并可离线使用,即使我们重新启动应用程序也是如此.这意味着我们的应用程序将使用存储在缓存中的本地数据在线工作.

如果数据在 setPersistenceEnabled(true); 时已经可以离线使用,为什么我需要 keepSynced(true) ?

If data will already be available offline when setPersistenceEnabled(true); , why do I need keepSynced(true) ?

DatabaseReference r = FirebaseDatabase.getInstance().getReference("chat/roomid");
r.keepSynced(true);

推荐答案

您应用中的 Firebase 数据库客户端可以将数据库中的数据保存在两个位置:内存和/或磁盘.

The Firebase Database client in your app can keep the data from the database in two places: in memory and/or on disk.

  1. 当您附加侦听器时,它会将数据从数据库同步到您应用中的内存表示.
  2. 如果您启用了持久性,数据将自动保存在磁盘上.
  3. 当您从某个位置分离最后一个侦听器时,该位置的数据将从内存中刷新.但它不会从磁盘中删除.

当您保持位置同步时,客户端实质上会向该位置附加一个空的侦听器.因此,应用程序中的数据将始终与服务器上数据库中的数据保持同步(只要有网络连接).如果您尚未启用持久性,则数据只会在内存中保持最新.如果您启用了持久性,它也会在磁盘上保持最新.

When you keep a location synchronized, the client essentially attaches an empty listener to that location. So the data in the app will always be up to date with what's in the database on the server (as long as there is a network connection). If you haven't enabled persistence, the data will just be kept up to date in memory. If you've enabled persistence, it will also be kept up to date on disk.

虽然将 keepSynced 与持久性一起使用是最常见的,但也有没有持久性的用例.

While it's most common to use keepSynced with persistence, there are also use-cases without persistence.

例如,如果您有一个主从应用程序,您经常在其中从项目名称列表跳转到每个项目的详细信息.在这种情况下,保持项目名称列表同步将使您不必在用户从详细信息屏幕返回时重新加载该数据.

For example, if you have a master-detail app, where you frequently bounce from a list of item names to the details of each item. In that case keeping the list of item names synchronized will save you from having to reload that data when the user comes back from the detail screen.

您当然也可以简单地为数据保留一个侦听器,这本质上是 keepSynced 在幕后所做的.

You could of course also simply keep a listener on the data, which is essentially what keepSynced does behind the scenes.

这篇关于Firebase:setPersistenceEnabled 和 keepSynced 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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