Search code examples
androidfirebasefirebase-realtime-databaseoffline-caching

Firebase offline capabilities causing memory problems


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:

Enabling disk persistance

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

and

Keeping a node synced

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

ChatNode.keepSynced(true);

What is the difference between the two?

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

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) If you write data offline: data is stored on disk and is sent to database when you go online again.

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) (keep synced true) will keep a database reference synced in 2 ways:

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) If you are using (keep synced) alone then you only store to what is known as the app memory.

The problem

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

The question

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.


Solution

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