19

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.
5
  • If you are clearing data(cache) then it won't work. Commented Nov 30, 2016 at 5:35
  • 1
    I am not clearing the app's data(cache), just kill the app.
    – Crocodile
    Commented Nov 30, 2016 at 5:38
  • Whether your 5th step is correct, depends on how you write the data in step 2. If it's a regular setValue() it indeed should have been persisted to disk. Commented Nov 30, 2016 at 16:33
  • 2
    I was not able to reproduce the problem using Firebase version 10.0.0. I performed the steps you listed and observed the new record added to the database (using Firebase console) when the app was relaunched. I also enabled debug logging to get better visibility on low-level Firebase operations, FirebaseDatabase.getInstance().setLogLevel(Logger.Level.DEBUG). The logcat messages are very detailed. You might find that output helpful.
    – Bob Snyder
    Commented Dec 1, 2016 at 15:47
  • I have performed the same test and found firebase sync's the data on restart. Using Firebase database 16.0.2
    – angryITguy
    Commented Sep 4, 2018 at 3:39

2 Answers 2

4
+25

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.

4
  • 1
    What's the point of offline transaction handling, when app restarts lose data ? This is not an edge case. Once again Google stopping at 80% of the required task.
    – angryITguy
    Commented Sep 4, 2018 at 1:25
  • @giulio I totally agree with you. Adding manual handling is rather difficult, but without it UX is broken.
    – Ufkoku
    Commented Sep 4, 2018 at 7:31
  • Since writing my comment above, I have retested the same scenario by updating the database offline, stopping the app ("force stop"), restarting the app, and then re-connecting, and found that in version 16 the data is getting sync'd. So, I'm a bit confuse as to how this user case is triggered.
    – angryITguy
    Commented Sep 4, 2018 at 23:32
  • 2
    The example code shown in the question does not use transactions. As such, the quoted paragraph does not apply. I've tested normal writes and they are stored and synced even if the app is killed while being offline. Commented Nov 10, 2019 at 23:19
3

Works like a charm , see what i did.

    ---actions when app is offline---
  • 1. Added 4 entries
  • 2. App killed
  • 3. edited 1 entry
  • 4. App killed
  • 5. Delete one entry
  • 6. App killed
  • 7. App starts this time with internet connection
  • Data was perfectly synched. I have used 4 lines of firestore magic

    FireBase Database

     //Firebase Database Magic lines
        val firebaseDatabase: FirebaseDatabase = FirebaseDatabase.getInstance()
        firebaseDatabase.setPersistenceEnabled(true)
        val firebaseDatabase: FirebaseDatabase= firebaseDatabase.getReference("Prakash")
        databaseReference.keepSynced(true)
    
    0

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.