dave08
dave08

Reputation: 63

Should a WorkManager be used with a SyncAdapter?

Previously one could assure a series of updates using an IntentService or SyncAdapter w/ a WakeLock.

Now with the new Doze mode, and limitations to save battery, is WakeLock still reliable enough, or should longer processes be started in the IntentService or SyncAdapter using WorkManager?

Also is WorkManager api production-ready?

Upvotes: 4

Views: 3943

Answers (2)

mbo
mbo

Reputation: 4691

As far as I understand it, the WorkManager can replace IntentService and SyncAdapter completely. E.g. everything you're doing in SyncAdapter.onPerformSync() can now be in WorkManager.doWork().

It got a stable release 2.4.0 and is ready for production.

Regarding wake locks: In Android 8.0 there is the following change:

As one of the changes that Android 8.0 (API level 26) introduces to improve battery life, when your app enters the cached state, with no active components, the system releases any wakelocks that the app holds.

So I wouldn't rely on wake locks anymore. I think they will be completely removed in future Android versions. And most of the time you don't need them with the WorkManager.

Upvotes: 7

Drusantia
Drusantia

Reputation: 327

I wouldn't bother using JobScheduler or AlarmManager. WorkManager sits on top of these, and handles everything for you, doesn't matter what android version your app runs on.

https://codelabs.developers.google.com/codelabs/android-workmanager Here's a good example about WorkManager, where they use a Worker for image processing, but it's perfectly good to do any long-running tasks. You can specify constraints on various things, including if you want the device to be idle to run the worker. You can also chain the workers, pass data from one to other, even group them and run some parallel, wait for all to finish and then continue with another (or more) worker(s). Depending on your use case, you can basically start a worker from anywhere (activity, broadcast receiver, stc).

In fact I use workers started from a broadcast receiver to do some api calls, only when you have internet connection of course (settable constraint) and it's so easy to set up and works so well, I only can recommend it to use (and bless Google for finally making these AC libraries). I also really like the fact that the WorkManager saves works to db with room, so it can pick it up whenever all the conditions check out, even if you restart the device between. If you have some monitoring set up (like for example stetho), you can actually see how it saves the jobs to its own database.

It's still in alpha, but it's so solidly built, I don't think they'll change too much until they release the final version.

Upvotes: 1

Related Questions