Handling keepSynced () in the background on Android and using FCM

I am considering using keepSynced() for some data from the Firebase Realtime database. I understand that it automatically syncs these paths. But how does this relate to the Android life cycle? If the user disables all actions (and disables all regular listeners), will the synchronization cease? I do not want the application to become a data source or battery.

On the other hand, I would like to update the cached data when the FCM is notified. I can start a service that will connect to Firebase. I would like to sync all the paths that are in keepSynced() and stop them when syncing. I am not sure how to achieve this. Create a listener on one of the paths and keep working for some time? After completing the service, will the synchronization stop?

+5
source share
1 answer

firebaser here

Great question!

If there is no active action, the operating system can close the connection to the Firebase database at any time. Our SDKs do not try to prevent this, but will reconnect when the application is activated again.

What you describe in your second paragraph is what we call push to sync, where you send a push notification (usually a quiet FCM data message) to initiate data synchronization.

We did something similar in the I / O application last year, and although it was a little more complicated than we wanted, it worked great. We explicitly controlled the connection in this case by calling goOnline() and goOffline() (after 5 minutes iirc). The main synchronization code can be found in the IOSched github repo .

+5
source

Source: https://habr.com/ru/post/1264175/


All Articles