0

I have an app that to displays new data in a notification. This data is provided by a network device. I can query this device and compare it the cached data in my service to determine if the notification needs to be updated. My app has a service that has local variables in which it caches the data. If the device has newer data, I present that in a notification to the user. Now I started to get IllegalStateException because Android O doesn't allow startService() gets called when the app is in background mode. I know I can start the service in Foreground mode, but since Android is providing less resource intensive ways, I would like to try something new. Next to the data being cached in local variables it gets stored in Firebase Database.

My requirements:

  1. Check every 10 seconds (if the screen is on) if there's new data
  2. Check if there's new data when the screen is switched on
  3. If there is new data, update the notification
  4. Needs to be long running and be able to compare new data to old (cached or from firebase) data.
  5. Can run when the app is in background mode

My thoughts: I've looked at Firebase job-dispatcher (https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-). Maybe I can configure it to run every time the screen is switched on, and every 10 seconds, to retrieve the new data from the network device and match that up with the data in Firebase database. But maybe it will cost a lot of performance to query the database that often.

Beuz
  • 193
  • 1
  • 12
  • *But maybe it will cost a lot of performance to query the database that often.* are you not doing that already? – Tim Sep 19 '17 at 09:20
  • No, at the moment the service has all the data in local variables I need to determine if there's new data. It also contains all the data the notification needs. Only when the cached data in the local variables is outdated, the data is updated in the database, and in the local variables, ofcourse. – Beuz Sep 19 '17 at 09:23
  • So your issue is that with the jobdispatcher not being a long running service, your cache will be invalidated radiply, causing you to have to refetch the data very often? – Tim Sep 19 '17 at 09:27
  • Exactly. I don't think I can cache the data in a jobdispatcher as I could in a service. The service can keep the data cached for hours, days even. – Beuz Sep 19 '17 at 09:29
  • Have you looked into alternative ways to cache your data? – Tim Sep 19 '17 at 09:34
  • Only way I see is to retrieve them from the database each time. – Beuz Sep 19 '17 at 09:54
  • So what type of caching are you thinking about? – Beuz Sep 24 '17 at 08:12

2 Answers2

0

Try Object based Database like Realm, SnappyDB(Uber uses it) etc. Queries are super fast

Ashik sunny
  • 56
  • 1
  • 1
  • Thanks for the idea, but I will stay with Firebase database. I like the realtime capabilities and performance is great. I have solved my issue by implementing a Firebase job dispatcher that promovates my service to the foreground, and the service holds the connection to my network device and the objects the notification needs. – Beuz Sep 29 '17 at 13:02
0

Solved this by keeping my service. Android Oreo has forced me to use this service different. Just a short description in case anyone needs it:

  1. My service is put in foreground mode whenever a connection to the network device is possible.
  2. Service is stopped when no connection to the network device is possible. At the same time a Firebase Jobdispatcher job is scheduled.
  3. When the jobdispatcher runs the job, it looks if a connection to the device is possible. If true -> the service is started and promotes itself to the foreground.
  4. The service still maintains the connection with the network device and holds the objects that the notification needs.
Beuz
  • 193
  • 1
  • 12