Reputation: 511
I am working on firebase for the first time, read about offline capabilities of firebase , tested two scenarios :
scenario 1 (working):
offline mode, writing data to firebase database.
press back button(closed app)
scenario 2 ( not working):
I added this line:
Firebase.getDefaultConfig().setPersistenceEnabled(true);
how to handle scenario 2 ? Do I need to handle this scenario through local database?
Upvotes: 7
Views: 3505
Reputation: 41
No need to handle scenario 2 using local database . Use Firebase.getDefaultConfig().setPersistenceEnabled(true) in application class and make android:name="yourapplicationclass" in manifest file. to handle sync while change network ie online/offline use transaction handler to handle local sync to firebase database since some tome data is not pushed to firebase server.Like this I used inside network change method and solved this issue:
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) {
}
});
Upvotes: 0
Reputation: 131
Are you using Firebase.getDefaultConfig().setPersistenceEnabled(true);
and keepSynced(true)
?
Because in Firebase documentation says that keepSynced(true)
it's who make the "magic" happens (together with setPersistenceEnabled(true)
):
By calling keepSynced(true) on a location, the data for that location will automatically be downloaded and kept in sync, even when no listeners are attached for that location. Additionally, while a location is kept synced, it will not be evicted from the persistent disk cache.
So, if you're not using it, you're not persisting your database locally and then when you "kill" the application, there will not be any database to query from when your app is opened again.
Upvotes: 1
Reputation: 1213
I guess your using some service to sync the data, It will not work for 2nd scenario. For that when user turn on data services you will receive a broadcast receiver, from that check service is not running then start the service.
Upvotes: 0