Firebase脱机功能导致内存问题 [英] Firebase offline capabilities causing memory problems

查看:52
本文介绍了Firebase脱机功能导致内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个聊天应用程序,该应用程序使用Firebase数据库存储数据.开发聊天应用程序时,通常的方法是保持数据库节点同步,以便您脱机访问消息.因此,当我实现Firebase脱机功能以保持数据节点同步时,问题就出现了.Firebase建议了两个脱机访问数据的必需步骤:

I am developing a chat app that uses firebase database to store data. The usual approach while developing a chat app is to keep the database nodes synced so that you access the messages offline. So the problem rises when I implement the firebase offline capabilities to keep the data nodes synced. Firebase suggests two required steps for accessing data offline:

启用磁盘持久性

这是根据文档通过使用以下代码行启用的(在我的情况下,我将其添加到应用程序类中):

this is enabled according to the documentation by using this line of code (in my case I add it in application class):

FirebaseDatabase.getInstance().setPersistanceEnabled(true);

保持节点同步

只需在您希望保持同步的任何数据库引用中添加keepsynced(true)即可​​启用此功能,如下所示:

this is enabled by simply adding keepsynced(true) to any databasereference that you wish to keep synced, like this:

ChatNode.keepSynced(true);

两者之间有什么区别?

根据Firebase团队在此站点上的答案,我推断出:

According to the firebase team answers on this site, I deduced that:

1)(磁盘持久性)将数据存储在设备磁盘上以在需要时使用它们,并且数据是在您写入数据或读取数据时存储的.

1) (Disk persistance) stores the data on the device disk to use them when needed, and data is stored wether you write data or read data.

a):如果您离线写入数据:数据存储在磁盘上,并在您再次在线时发送到数据库.

a) If you write data offline: data is stored on disk and is sent to database when you go online again.

b):如果您离线读取数据:在线读取并保留在磁盘中并存储的侦听器,您将能够从磁盘离线读取数据.

b) If you read data offline: the listener that was read online and was kept in disk and stored, you will be able to read it offline from disk.

2)(保持同步为真)将以两种方式保持数据库引用的同步:

2) (keep synced true) will keep a database reference synced in 2 ways:

a)如果您还将(磁盘持久性)与(保持同步)一起使用,您将能够使数据在磁盘上保持同步...这似乎是(磁盘持久性)的默认行为).

a) If you are also using (disk persistence) with (keep synced) you will be able to keep data synced on disk ... which seems to be the default behavior of (disk persistence).

b)如果单独使用(保持同步),则仅存储到所谓的应用程序内存中.

b) If you are using (keep synced) alone then you only store to what is known as the app memory.

问题

我确实设置了这两种方法,但是我的应用现在非常缓慢,缓慢,有时会自行停止.

I did set both of the methods, but my app is now very laggy and slow and sometimes stops on its own.

问题

如果我上面说的所有事情都是正确的,那么这种脱机功能方法会对我的应用程序造成沉重负担吗?

If all the things that I said above are true, then would this method of offline capability be a heavy load on my app?

如果我保持了许多侦听器的同步并启用了持久性,那么磁盘会充满数据吗?我应该清理数据吗?两种方法都可以自行清除磁盘上的数据吗?数据是否会从内存中自动清除?

If I kept many listeners synced and set persistence enabled, then would the disk become full of data? Should I clean the data? Is the data on disk cleaned by itself in both methods? Is data cleaned by itself from memory?

感谢您的帮助,我想避免我的应用程序出现滞后和响应缓慢的情况.

I want to avoid the lagging and slow response in my app, thanks for your help.

推荐答案

您的假设是正确的.如果您使用的是 FirebaseDatabase.getInstance().setPersistenceEnabled(true); ,则表示Firebase将创建数据库的本地副本,这也意味着将添加脱机时所做的所有更改到 queue .因此,随着此队列的增长,本地操作和应用程序启动将变慢.因此,速度取决于该队列的维数.但是请记住,Firebase被设计为在线数据库,可以在断开连接的短时间内工作,而不是作为离线数据库.

You are right about your assumptions. If you are using FirebaseDatabase.getInstance().setPersistenceEnabled(true); means that Firebase will create a local copy of your database which also means that every change that is made while you are offline, will be added to a queue. So, as this queue grows, local operations and application startup will slow down. So the speed depends on the dimension of that queue. But rememeber, Firebase is designed as an online database that can work for short to intermediate periods of being disconnected and not as an offline database.

第二,如果正在使用许多侦听器,请不要忘记根据活动的生命周期删除侦听器,如下所示:

Second, if are using many listeners, don't forget to remove the listener accordingly to the life-cycle of your activity like this:

databaseReference.removeEventListener(valueEventListener);

这篇关于Firebase脱机功能导致内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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