0

I am trying out Firebase Realtime Database. I have a created a node users, In that I have created a node user_1. The JSON is like the one below

{
  "user_1" : {
    "firstName" : "User",
    "lastName" : "Last",
    "middleName" : "S"
  }
}

I have enabled Offline Persistence & the Sync enabled for that particular Node user_1.

FirebaseDatabase.getInstance().setPersistenceEnabled(true);
FirebaseDatabase.getInstance().getReference().child("users").child("user_1").keepSynced(true);

Firstly , I opened the App & synched the user_1 data. Then I killed the Application, But the Device is still connected to internet. I made changes to the user_1 node in the Firebase Console. After sometime , I have disconnected to internet. I opened the app again, listened to that node user_1. But the offline data is not synced with the latest data from Firebase Server. Does that mean , keepSynced() will only work when the Application is in Foreground ?

Sasank Sunkavalli
  • 3,864
  • 5
  • 31
  • 55
  • Yes, because FirebaseDatabase.getInstance() is mapped with your application class and when you kill your application firebase will not get any callback. Please go through to this [link](http://www.vogella.com/tutorials/AndroidLifeCycle/article.html) – yashkal Dec 08 '17 at 11:51
  • Where are you removing the listener? – Alex Mamo Dec 08 '17 at 12:30
  • I am removing the Listener in the Application, But from the docs , what i understand , If we use keepSynced(true), then the data should be synced without any listeners – Sasank Sunkavalli Dec 08 '17 at 13:23
  • @AlexMamo `keepSynced(true)` essentially set an empty listener on the reference/query. You'd usually not remove it. – Frank van Puffelen Dec 08 '17 at 14:13
  • @FrankvanPuffelen I understand, beeing empty it's not need to be removed. Thank you! – Alex Mamo Dec 08 '17 at 14:26

1 Answers1

1

When you call keepSynced(true), the Firebase Realtime Database client synchronizes the data at that location while your app is active.

If you force-kill the app, the client will not be active anymore and it won't synchronize anymore.

It may continue to synchronize for a while when your back goes into the background. Whether it does this and for how long depends on the Android version and device manufacturer. Given their efforts to reduce background data usage (to improve battery life), I'd recommend not relying on this.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • But if I want to listen to the Database change events from Background , is there any way we can achieve it using Firebase Realtime Database ? – Sasank Sunkavalli Dec 08 '17 at 14:24
  • 1
    That has been covered quite a few times before, so I recommend you check out some of those [previous questions](https://www.google.com/search?q=site:stackoverflow.com+firebase+database+android+background). For example: [Handling keepSynced while on background on Android and with FCM](https://stackoverflow.com/q/42210186) and [Firebase Read data in background Android](https://stackoverflow.com/q/42606370) seem similar. – Frank van Puffelen Dec 08 '17 at 14:33