How to handle standalone firebase database when destroying android application?

This is my first time working on firebase, reading about the standalone capabilities of firebase, and tested two scenarios:

scenario 1 ( working ):

  • offline mode, writing data to the firebase database.

  • press the return button (closed application)

  • went online, the data was added to the firebase database.

scenario 2 ( does not work ):

  • offline mode, writing data to firebase database
  • close application
  • remove the application from the background (killed the application)
  • went online, data is not added.

I added this line:

Firebase.getDefaultConfig().setPersistenceEnabled(true); 

how to handle scenario 2? Do I need to process this script through a local database?

+5
source share
3 answers

I assume that you are using some service for data synchronization, it will not work for the second scenario. To do this, when the user turns on the data services, you will receive a broadcast receiver from which the verification service will not start, start the service.

0
source

Do you use Firebase.getDefaultConfig().setPersistenceEnabled(true); and keepSynced(true) ?

Because in the Documentation, Firebase says that keepSynced(true) happens to the one who does the โ€œmagicโ€ (along with setPersistenceEnabled(true) ):

When keepSynced (true) is called in a location, the data for that location will be automatically downloaded and synchronized , even if no listeners are connected to that location. In addition, as long as the location is saved, it will not be removed from the cache of the permanent drive .

So, if you do not use it, you do not save your database locally, and then when you โ€œkillโ€ the application, there will be no database to query when your application will be opened again.

0
source

There is no need to handle scenario 2 using a local database. Use Firebase.getDefaultConfig (). SetPersistenceEnabled (true) in the application class and create an android: name = "yourapplicationclass" in the manifest file. to handle synchronization when the network changes, i.e. an online / offline transaction handler to handle local synchronization with the firebase database, as some tome data does not fit on the firebase server. Similarly, I used the network replacement method and solved this problem:

 mDatabase.runTransaction(new Transaction.Handler() { @Override public Transaction.Result doTransaction(MutableData mutableData) { if(mutableData.getValue() == null) { mutableData.setValue(1); } else { mutableData.setValue((Long) mutableData.getValue() + 1); } return Transaction.success(mutableData); //we can also abort by calling Transaction.abort() } @Override public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) { } }); 
0
source

All Articles