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

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

问题描述

我认为,当我使用以下全部数据进行聊天对话的整个时间将随时可以离线使用。哪个不是,所有节点都从服务器加载。

  FirebaseDatabase.getInstance()。setPersistenceEnabled(true); 

然后,按照DOCS:
持久性行为:通过启用持久性,即使在重新启动应用程序时,我们在线同步的任何数据都会持久保存到磁盘并可脱机使用。这意味着我们的应用程序将像在线使用存储在缓存中的本地数据一样工作。


如果数据已经存在当$ setPersistenceEnabled(true); ,为什么需要 keepSynced(true)

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


解决方案

您的应用中的Firebase数据库客户端可以保留数据从数据库中的两个地方:内存和/或磁盘上。


  1. 当您附加一个侦听器时,它将数据库中的数据同步到在您的应用程序中的内存表示。

  2. 如果您启用了持久性,那么数据也会自动在硬盘上保存。

  3. 来自某个位置的侦听器,该位置的数据将从内存中清除。但不会从磁盘中删除。

当您保持一个位置同步时,客户端基本上将一个空的侦听器附加到该位置。因此,应用程序中的数据将始终与服务器上的数据库中的内容保持一致(只要存在网络连接)。如果你没有启用持久性,数据将会保持在内存中。如果你已经启用了持久性,它也会在磁盘上保持最新。



虽然使用keepSynced和persistence是最常见的,但是也有一些用例持久性。例如,如果您有一个主从应用程序,那么您经常从项目名称列表中跳转到每个项目的详细信息。在这种情况下,保持项目名称的同步列表将使您不必在用户从细节屏幕返回时重新加载数据。



当然,您也可以简单地在数据上保留一个监听器,这就是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);

Then, according to DOCS :

Persistence Behavior : By enabling persistence, any data that we sync while online will be persisted to disk and available offline, even when we restart the app. This means our app will work as it would online using the local data stored in the cache.

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

解决方案

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

  1. When you attach a listener, it synchronizes data from the database to an in memory representation in your app.
  2. If you've enable persistence, the data is automatically also persisted on disk.
  3. When you detach the last listener from a location, the data for that location is flushed from memory. But it is not deleted from disk.

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.

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.

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