keepSynced() and Android activity lifecycle

504 views
Skip to first unread message

Martin d'Allens

unread,
Dec 8, 2016, 1:13:57 AM12/8/16
to fireba...@googlegroups.com
Hi Firebase,

What is the recommended usage of keepSynced() with respect to the Android activity lifecycle?
I would ideally set keepSynced(true) on all low-traffic nodes that are of interest for local caching as soon as the app starts, to save server round trip times when navigating between activities.
From what I understand this would have the same energy-usage implications as setting a listener on each of the nodes, which means that a network connection will be kept open. This sounds like a bad idea.
So do you recommend to set keepSynced(false) in the onPause()?

Martin

Kato Richardson

unread,
Dec 9, 2016, 3:40:19 PM12/9/16
to Firebase Google Group

Hi Martin,

Honestly, you’re probably doing more harm than good trying to optimize energy usage here. Firebase and Android probably do better on their own. For example, Android (and iOS, too) will kill network connections for applications which are backgrounded, which probably makes your attempts here to cut down on use of the socket connections redundant.

Additionally, keepSynced(true) is intended to be used with offline persistence. So when you sync a path, it writes that data to local storage, which is a fairly expensive event from a battery life standpoint. But by calling keepSynced(false) when pausing the app, you would effectively tell Firebase that you don’t want that data cached to disk, meaning it would likely be purged. Thus, you might incur two writes to disk without any substantial gain.

Instead, I’d just subscribe using normal event listeners in onResume() and cancel them in onPause(). They will still survive temporary losses of connectivity, and since you’re telling Firebase not to store data when the app is paused/killed, you’ll get the same behavior.

If you happen to know when the user is looking at Firebase realtime data, and when they are not, then there is one optimization that could actually help battery life: Firebase keeps a socket open in a separate thread on Android, so if only some activities are utilizing the connection, you can call goOnline() and goOffline() in onResume/onPause to manually disconnect from the Firebase servers. This would sever the connection faster than the Android OS timeout for activity, and tell Firebase that there’s no need to reconnect immediately when the socket becomes available again. 

Note that the gains here are probably insignificant as well, but it’s at least likely to be a positive gain.

☼, Kato


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/CAMBZ%2Bfywo4v3HD4oUTmAY%3Dtg%2BbSoAo9Gciig-1y8n0JfKKwvbw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Doug Stevenson

unread,
Dec 9, 2016, 6:10:36 PM12/9/16
to Firebase Google Group
Martin,

Another thing to look into is the use of FirebaseApp.setAutomaticResourceManagementEnabled.  Passing true there should help Firebase manage connections on older versions of Android that don't enforce some of the best practices that Kato mentioned, when you have listeners that are still active when an app is no longer visible.

Doug


On Friday, December 9, 2016 at 12:40:19 PM UTC-8, Kato Richardson wrote:

Hi Martin,

Honestly, you’re probably doing more harm than good trying to optimize energy usage here. Firebase and Android probably do better on their own. For example, Android (and iOS, too) will kill network connections for applications which are backgrounded, which probably makes your attempts here to cut down on use of the socket connections redundant.

Additionally, keepSynced(true) is intended to be used with offline persistence. So when you sync a path, it writes that data to local storage, which is a fairly expensive event from a battery life standpoint. But by calling keepSynced(false) when pausing the app, you would effectively tell Firebase that you don’t want that data cached to disk, meaning it would likely be purged. Thus, you might incur two writes to disk without any substantial gain.

Instead, I’d just subscribe using normal event listeners in onResume() and cancel them in onPause(). They will still survive temporary losses of connectivity, and since you’re telling Firebase not to store data when the app is paused/killed, you’ll get the same behavior.

If you happen to know when the user is looking at Firebase realtime data, and when they are not, then there is one optimization that could actually help battery life: Firebase keeps a socket open in a separate thread on Android, so if only some activities are utilizing the connection, you can call goOnline() and goOffline() in onResume/onPause to manually disconnect from the Firebase servers. This would sever the connection faster than the Android OS timeout for activity, and tell Firebase that there’s no need to reconnect immediately when the socket becomes available again. 

Note that the gains here are probably insignificant as well, but it’s at least likely to be a positive gain.

☼, Kato

On Wed, Dec 7, 2016 at 2:07 PM, Martin d'Allens <martin....@liberty-rider.com> wrote:
Hi Firebase,

What is the recommended usage of keepSynced() with respect to the Android activity lifecycle?
I would ideally set keepSynced(true) on all low-traffic nodes that are of interest for local caching as soon as the app starts, to save server round trip times when navigating between activities.
From what I understand this would have the same energy-usage implications as setting a listener on each of the nodes, which means that a network connection will be kept open. This sounds like a bad idea.
So do you recommend to set keepSynced(false) in the onPause()?

Martin

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages