Will WorkManager's queue be cleared in case of an app update?
Asked Answered
B

2

7

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?

Bogart answered 15/10, 2020 at 9:4 Comment(0)
P
6

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.

Plus answered 16/10, 2020 at 17:14 Comment(0)
A
5

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);
}
Akee answered 15/10, 2020 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.