0

I just realized that with a single line of code I can be able to use my app Online and Offline to access the Firebase Database ( learned here ).

I'm developing an app, as an MVP and Firebase study, and I'm using this two methods to grant access offline:

FirebaseDatabase database = FirebaseDatabase.getInstance();
database.setPersistenceEnabled(true);

DatabaseReference reference = getFirebaseDatabaseInstance().getReference();
reference.keepSynced(true);

So, my question is :

It's correct to use both of this methods ? Or should I use only setPersistenceEnabled(true) ?

I noticed that if I use keepSynced(true) my app avoids loading every time I change into another Activity / Fragment.

Marcello Câmara
  • 187
  • 1
  • 13

1 Answers1

0

setPersistenceEnabled(true) and keepSynced(true) do two different things.

setPersistenceEnabled(true) turns on disk caching of query results. Whenever possible, cached results will be used instead of fetching them from the server again.

keepSynced(true) is the exact same thing as adding a listener to the same location, and not removing it. So, whenever updates are available to that location, the SDK will download them and invoke any other listeners that are interested in that data.

Whether or not you should use either one of them is dependent on whether or not you want their stated purpose. Their functionalities don't overlap, except in that if both are enabled, any updates for the keepSynced location are also cached on disk, which just logical when you add up their functionalities.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • would `keepSynced` use lots of resources since it can effectively be listening to something for all users in the database? – uuuuuu Oct 26 '20 at 13:09