Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase does not sync offline cache if the app is killed

I am setting offline persistence

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

as described in an earlier post, but the following use case fails:

  1. Turn internet connectivity OFF on handset
  2. Attempt writing to the DB
  3. Kill app from the memory using the users' multitasking menu in the OS
  4. Turn internet connectivity back ON
  5. Relaunch the app. At this point I expect the new record from step 2 to be sent to the DB via the restored network connectivity, but this does not happen. (Are my expectations correct?)

Sample code:

static{
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}

void updateValue(){
    DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("mydb");
    dbRef.keepSynced(true);
    dbRef.setValue("123");
}

Note that, if I don't kill the app from memory the caching works:

  1. Turn internet connectivity OFF on handset
  2. Attempt writing to the DB
  3. Turn internet connectivity back ON
  4. The new record is sent to the DB once the network connectivity is restored.
like image 496
Crocodile Avatar asked Nov 30 '16 05:11

Crocodile


1 Answers

According to firebase documentation

Transactions are not persisted across app restarts

Even with persistence enabled, transactions are not persisted across app restarts. So you cannot rely on transactions done offline being committed to your Firebase Realtime Database. To provide the best user experience, your app should show that a transaction has not been saved into your Firebase Realtime Database yet, or make sure your app remembers them manually and executes them again after an app restart.

like image 107
Ufkoku Avatar answered Sep 20 '22 02:09

Ufkoku