WAI
Status Update
Comments
ra...@google.com <ra...@google.com> #2
I'm not sure how to edit the description. But forgot to add that this will work when the app is in the foreground. It just doesn't work when you lock your device. I test this by moving the app to the background and then locking my device and checking the logs in Android Studio.
Description
Component used: WorkManager
Version used: 2.10.0
Devices/Android versions reproduced on: Emulator
Observed behavior:
When a running Worker is updated with
updateWork
to change its constraints to values that are not satisfied at that current moment the worker keeps running.Expected behavior:
The updated worker is cancelled if the new constraints are not satisfied.
This behavior doesn't match the documentation which reads:
The observed behavior is also inconsistent with the situation when constraints themselves change while a Worker is running (e.g. network change) - the Worker will cancel if they are no longer satisfied.
Sample code:
Real world example:
Many apps that transfer large amounts of data (e.g. podcast players, file synchronization) have a setting to select which network type to use for heavy transfers: any or unmetered. When the user changes the setting back and forth while exploring the app they risk that a background sync Worker gets started.
This potentially causes the user some cost, even if they realize their mistake immediately and change the setting back to "unmetered only" because the Worker will keep running.
This issue can be worked around by adding some logic to
CANCEL_AND_REENQUEUE
the existing Worker whenUpdateResult
isAPPLIED_FOR_NEXT_RUN
.