How to change periodic work request period without it running immediately using WorkManager?
Asked Answered
R

3

9
val request = PeriodicWorkRequestBuilder<FooWorker>(1, TimeUnit.DAYS).build()
WorkManager.getInstance().enqueueUniquePeriodicWork(
    "FOO",
    ExistingPeriodicWorkPolicy.REPLACE,
    request
)

The code above is ran in onCreate of Application to ensure the request is enqueued. However, this will cause a problem where the FooWorker will run every time user start the application because ExistingPeriodicWorkPolicy.REPLACE cancel previous work and enqueue new work which cause it run immediately.

If we change to ExistingPeriodicWorkPolicy.KEEP, we will not able to replace the work even the period or the worker is changed.

Is there a way to replace the request after the current one is running?

For example, origin request run a 1 time per day and the new request is 1 time per hour. After the next origin request is ran, then replace it with new request.

Rennin answered 10/10, 2018 at 3:35 Comment(0)
I
8

There is no way to do exactly what you want with periodic work in a clean way.

However, there's absolutely no need to use periodic work itself. The same structure can be easily accomplished by scheduling the next WorkRequest at the end of your doWork method, right before returning Result.SUCCESS:

fun doWork(): Result {
  reallyDoWork()
  // Now schedule the next "periodic" work
  val request = OneTimeWorkRequestBuilder<FooWorker>().build()
  WorkManager.getInstance().enqueueUniqueWork(
    "FOO",
    ExistingWorkPolicy.REPLACE,
    request
  )
  return Result.SUCCESS
}

With this setup, your onCreate() of Application can safely use ExistingWorkPolicy.KEEP to avoid rescheduling work if you already have a WorkRequest queued up and when that queued up work fires, the next WorkRequest will be queued up with the appropriate new period.

Indonesia answered 10/10, 2018 at 5:38 Comment(1)
i can't see how you can set the duration of the time until it starts? or am i missing something?Ithaman
B
3

Looks like there is a way to replace a periodic work now with enqueueUniquePeriodicWork.

  val request = PeriodicWorkRequest.Builder(FooWorker::class.java, 1, TimeUnit.DAYS).build()
  WorkManager.getInstance(appContext)
  .enqueueUniquePeriodicWork(WORK_TAG, ExistingPeriodicWorkPolicy.REPLACE, request)

Make sure you are passing ExistingPeriodicWorkPolicy instead of ExistingWorkPolicy

Bozovich answered 10/4, 2019 at 16:39 Comment(0)
H
2

If you want to use ExistingPeriodicWorkPolicy.KEEP and to update PeriodicWorkRequest only when you want to change repeat interval, there is no solution from WorkManager, but you can save interval in sharedPreferences and check if interval was changed. And based on that to use ExistingPeriodicWorkPolicy.KEEP or ExistingPeriodicWorkPolicy.REPLACE

public static void enqueue(Context context) {
        Log.d(TAG, "enqueue()");
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        int repeatInterval = sharedPreferences.getInt("REPEAT_INTERVAL_HOURS", 0);

        PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest
            .Builder(FooWorker.class, REPEAT_INTERVAL_HOURS, TimeUnit.HOURS)
            .build();

        ExistingPeriodicWorkPolicy policy = repeatInterval == REPEAT_INTERVAL_HOURS ? ExistingPeriodicWorkPolicy.KEEP : ExistingPeriodicWorkPolicy.REPLACE;

        sharedPreferences.edit().putInt("REPEAT_INTERVAL_HOURS", REPEAT_INTERVAL_HOURS).apply();

        WorkManager.getInstance(context).enqueueUniquePeriodicWork("fileRemove", policy, periodicWorkRequest);
    }
Hose answered 26/9, 2020 at 22:45 Comment(1)
I'm doing the same once I got my API result in doWork(), but for me doWork() is calling again. So it going to loop.Cargill

© 2022 - 2024 — McMap. All rights reserved.