2

I am making an android app, that mostly runs in the background, communicating with firebase real-time database. My apps need to be in sync with firebase database all the time. Currently, I am only doing database write operations in my service. I have been using FCM for syncing database states, but FCM is not very reliable in my case.

Will it be wise decision to listen for database changes by attaching a listener to firebase database? By attaching a listener, I think it will keep an open connection with the database, which might increase battery usage.

My concern here is related to battery usage.

Abhishek Batra
  • 1,539
  • 1
  • 18
  • 45

3 Answers3

0

I think you should opt for Intelligent Job-Scheduling for Android

Scheduling this work intelligently can improve your app’s performance, along with aspects of system health such as battery life. JobScheduler does this scheduling work for you.

In addition to JobScheduler, there are several other facilities available to help your app schedule work. These include:

SyncAdapter can help you keep syncing between data

Also please consider

Firebase JobDispatcher can be used when on several time period you want to check for database update or feature like backup data on scheduled time.

Please read more at this link on Official Site

Deep Patel
  • 2,584
  • 2
  • 15
  • 28
  • JobScheduler will have to poll data and check if there is any change in data. This will cause late sync in data which will result in stale data on my data at times. – Abhishek Batra Apr 02 '18 at 07:29
  • I understand your concern, but have you read about that Sync Adapter? It may help you keeping it synced with minimal delay. https://developer.android.com/reference/android/content/AbstractThreadedSyncAdapter.html – Deep Patel Apr 02 '18 at 07:32
  • @AbhishekBatra : see that other answer also stating the same as I did. Its the best fit solution for your problem, I already had made chat application and been using this already. – Deep Patel Apr 02 '18 at 07:43
  • I know Sync Adapter might solve it, but there are issues with SyncAdapter on a couple of brands like Xiaomi, OnePlus etc. Hence, I cannot use Sync Adapters with my current use case. I am more into finding the consequences of using Firebase Real-time Database in a service, on the overall battery life of the device. Thanks for your help. – Abhishek Batra Apr 02 '18 at 08:53
0

Android provides different APIs for scheduling task.

  1. JobScheduler
  2. AlarmManager
  3. Firebase JobDispatcher

Check this about Job Scheduling.

All of them have their benefits and downsides. JobScheduler became available from Android 5.0 (API level 21). Firebase JobDispatcher needs Google Play preinstalled.

You can use Android-Job library. This library schedule jobs depending on your requirements. This library decides which API suits your job. For more details check Android-Job library. Its easy to use.

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
0

If you are using in your code listeners that are not removed according to the life-cycle of the activity, you need to know that your app will keep an open connection with the database and will increase the battery usage for sure.

Because also of the newtowrk traffic, Android will stop your process from networking, when it's not longer visible in the foreground. This is to prevent poorly behaved apps from consuming too many resources. When a process like this can be killed, depends on the Android version and device manufacturer. Given their efforts to reduce background data usage (to improve battery life) and I'd recommend not relying on this.

If your apps needs to continue networking when the user is no longer using it, you'll have to start a foreground service, which also requires that you show a notification that indicates to the user that your app is still running.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193