At every app start I enqueue periodic work using ExistingPeriodicWorkPolicy.KEEP, so that no additional work is queued if there is already a schedule.
Let's assume that in a future app update, I change the constraints or periodicity of the work, but keep the original uniqueName
.
will this change be ignored because the PeriodicWorkRequest
uses the same uniqueWorkName
?
Or will this not be a problem because all work for an app is cancelled when the app is updated?
What's the best approach here?
When you update your app the scheduled work is maintained.
If you want to modify the workRequest
the only option is to cancel the previous request and register a new one.
I don't see this as a WorkManager specific problem as a new application version may require to do some action just once (onboarding a new feature, migrating some preferences, modifying some WorkManager work, etc). There are some useful answers using SharedPreferences in this question.
I would add this WorkRequest change in the section of your application that handles upgrades.
As WorkManager | Android Developer stated, You can set specific Tag
to WorkRequest
in addition to uniqueWorkName
. Indeed, you can preserve uniqueWorkName
without worrying about it.
When you need to change constraints of the WorkRequest
you need to first, Eliminate the old-fashioned WorkRequest
from the queue then add a new-fashioned one to the queue.
Here is some piece of code
PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(FooWorker.class)
.addTag("m_TAG_Version_1")
.build();
workManager
.enqueueUniquePeriodicWork("uniqueWorkName", ExistingPeriodicWorkPolicy.KEEP, request);
Then in future
if(isWorkUpdated)
{
workManager.cancelAllWorkByTag("m_TAG_Version_1");
PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(FooWorker.class)
.addTag("m_TAG_Version_2")
.build();
workManager
.enqueueUniquePeriodicWork("uniqueWorkName", ExistingPeriodicWorkPolicy.KEEP, request);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With