0

I am using Firebase Realtime Database for my android application functions. I am writing data to Firebase database each second whether app is online or offline on Some action for an specified amount of time.

If writing operation is performed in offline then, I want to perform some action when these data successfully synced to Firebase server.

FYI, I am using JobScheduler which will start once Network Connectivity available, after that I am also able to listen firebase connection status using .info/connected path.

User may have killed and restarted the app meanwhile.

But after that how can I ensure that all data synced to server successfully?

Randheer
  • 984
  • 6
  • 24

2 Answers2

1

When you call setValue(...) on a reference, you can pass in an optional CompletionListener(). When your data has been synchronized with (or rejected by) the Firebase servers, the completion listener's onComplete(...) method will be called. Also see my answer here for an example: Firebase Android - how to tell if node has been synced

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Thanks, but this is only working if app is not killed, but in my case user can kill and restart app. Is there any way I can determine sync status in this scenario? – Randheer Feb 01 '18 at 14:45
  • 1
    There is no way for completion listener to survive app restarts. Sorry about that. – Frank van Puffelen Feb 01 '18 at 15:22
0

When users go offline, the Realtime Database SDKs use local cache on the device to serve and store changes. When the device comes online, the local data is automatically synchronized.

  • Yes, I want to do some function when local data is synchronized automatically, how can i determine that. User may have kill and restarted the app meanwhile. – Randheer Feb 01 '18 at 14:41
  • Firebase apps remain responsive even when offline because the Firebase Realtime Database SDK persists your data to disk. Once connectivity is reestablished, the client device receives any changes it missed, synchronizing it with the current server state. – Srinivas Feb 01 '18 at 14:49
  • Yes, But I want to ensure changes made by my devices is synced with server. – Randheer Feb 01 '18 at 14:53