Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 966
Nino van Hooff Avatar asked Dec 30 '22 20:12

Nino van Hooff


2 Answers

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.

like image 109
pfmaggi Avatar answered Jan 15 '23 04:01

pfmaggi


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);
}
like image 43
A Farmanbar Avatar answered Jan 15 '23 05:01

A Farmanbar