WorkManager
and Side Effects
WorkManager
has a fairly clean and easy API and hides a lot of the complexity of scheduling background work that is not time-sensitive.
However, it has side effects.
To be able to restart your scheduled work after a reboot, WorkManager
registers an ACTION_BOOT_COMPLETED
receiver named androidx.work.impl.background.systemalarm.RescheduleReceiver
. To be a good citizen, WorkManager
only enables that receiver when you have relevant work and disables it otherwise. That way, your app does not unnecessarily slow down the boot process if there is no reason for your app to get control at boot time.
However, enabling and disabling a component, such as a receiver, triggers an ACTION_PACKAGE_CHANGED
broadcast. Few apps directly have any code that watches for this broadcast, let alone would be harmed by having that broadcast be sent more times that might otherwise be necessary.
App widgets, though, are affected by ACTION_PACKAGE_CHANGED
. Specifically, ACTION_PACKAGE_CHANGED
triggers an onUpdate()
call to your AppWidgetProvider
.
That too may not be a problem for most app widgets. Ideally, your AppWidgetProvider
makes no assumptions about when, or how frequently, it gets called with onUpdate()
. However, there is one area where this is a problem: with an AppWidgetProvider
scheduling work in onUpdate()
. The flow then becomes:
- A “regular”
onUpdate()
call comes in - Your
AppWidgetProvider
schedules some work withWorkManager
-
WorkManager
enablesRescheduleReceiver
- That triggers
ACTION_PACKAGE_CHANGED
, which triggers anonUpdate()
call - Your
AppWidgetProvider
schedules some work withWorkManager
again -
WorkManager
eventually gets through those two pieces of work -
WorkManager
disablesRescheduleReceiver
, since it is no longer needed - That triggers
ACTION_PACKAGE_CHANGED
, which triggers anonUpdate()
call - Your
AppWidgetProvider
schedules some work withWorkManager
-
WorkManager
enablesRescheduleReceiver
- And we’re in an infinite loop
The recommendation from Google is to avoid unconditionally scheduling work with WorkManager
from onUpdate()
. Instead, only do it if you know that the work is needed and that it is safe to do so, meaning that you will not get into the infinite loop.
That advice may be difficult for some to implement.
This, and any other possible side-effects of WorkManager
, are not documented. So, you need to be a bit careful about your use of WorkManager
:
- If your app responds to
ACTION_PACKAGE_CHANGED
broadcasts, directly or indirectly, it may not be safe to schedule work there, lest you wind up in the infinite loop scenario described above. - If your app responds to
ACTION_BATTERY_OK
,ACTION_BATTERY_LOW
,ACTION_POWER_CONNECTED
,ACTION_POWER_DISCONNECTED
,ACTION_DEVICE_STORAGE_LOW
,ACTION_DEVICE_STORAGE_OK
,CONNECTIVITY_CHANGE
,ACTION_TIME_SET
, orACTION_TIMEZONE_CHANGED
, bear in mind thatWorkManager
has receivers for those broadcasts in your app. These are all disabled at the outset, but presumablyWorkManager
has code to enable them based on certain conditions, such as certain constraints that you set in your work requests. Be careful about scheduling work withWorkManager
on those broadcasts as well. - If your app responds to
ACTION_BOOT_COMPLETED
broadcasts, bear in mind thatWorkManager
also depends on this broadcast. Your respective receivers might be invoked in any order. It may not be safe to schedule work here, asWorkManager
might assume that its ownACTION_BOOT_COMPLETED
receiver has completed its work by the time you try scheduling new work. While I would not expect an infinite loop scenario, this is the sort of edge case that requires a lot of testing to ensure everything will work as expected. -
WorkManager
has aContentProvider
that it bakes into your app as well. While scheduling your own work fromonCreate()
of aContentProvider
would be rather odd, it’s possible that somebody might want to do that. Be careful, asWorkManager
may not be fully ready for operation at that point. Note that, last time I tested it, allContentProvider
instances are created beforeonCreate()
ofApplication
, so probably it is safe to schedule work there.
There may be other edge and corner cases beyond these. So, while WorkManager
is nice, make sure that you thoroughly test your use of it.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.