Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Work Manager - Difference between ExistingPeriodicWorkPolicy.KEEP and .Replace

By referring to: Android WorkManager api for running daily task in Background

It uses the WorkManager.enqueueUniquePeriodicWork to ensure that PeriodicWorkRequest is not created multiple times.

example code:

val work = PeriodicWorkRequestBuilder<SyncWork>(15,TimeUnit.MINUTES).build()

WorkManager.getInstance().enqueueUniquePeriodicWork("TaskTag",
                                       ExistingPeriodicWorkPolicy.KEEP, work);

However, I found it there is 2 option of ExistingPeriodicWorkPolicy which is ExistingPeriodicWorkPolicy.KEEP and ExistingPeriodicWorkPolicy.REPLACE can be use.

I try to implement it and run the code, but it does really show any differences, and it seem both of them behave the same way.

My uncertainty:

How does the ExistingPeriodicWorkPolicy.KEEP perform differently from ExistingPeriodicWorkPolicy.REPLACE?

like image 243
I am a Student Avatar asked Aug 01 '18 07:08

I am a Student


People also ask

What is Flex interval in work manager?

This second interval (the flexInterval) it's positioned at the end of the repetition interval itself. Let's look at an example. Imagine you want to build a periodic Work request with a 30 minutes period. You can specify a flexInterval, smaller than this period, say a 15 minute flexInterval.

Can WorkManager be used to repeat jobs?

Retry and backoff policyIf you require that WorkManager retry your work, you can return Result. retry() from your worker. Your work is then rescheduled according to a backoff delay and backoff policy. Backoff delay specifies the minimum amount of time to wait before retrying your work after the first attempt.

Is WorkManager deprecated?

This method is deprecated. Call getInstance instead.

What's the best tool for handling work that is deferrable and expected to run even if your app restarts?

WorkManager is the recommended solution for persistent work. Work is persistent when it remains scheduled through app restarts and system reboots. Because most background processing is best accomplished through persistent work, WorkManager is the primary recommended API for background processing.


2 Answers

In your example, you are about to enqueue a new work request (a.k.a. worker). If you have done this just before at runtime, this worker is already present with state ENQUEUED.

KEEP: If a previous worker exists, your new attempt is simply ignored, else your new worker is enqueued.

REPLACE: If a previous worker exists, it is cancelled, leading to a state CANCELLED for it. Then or else, your new worker is enqueued.

So, if you are sure that your new worker is the same as the previous one (say the constraints have not changed), then KEEP should be safe, otherwise REPLACE might be the better option.

like image 171
nst0022 Avatar answered Sep 25 '22 20:09

nst0022


KEEP: If there is existing pending work with the same unique name, do nothing.

REPLACE: If there is existing pending work with the same unique name, cancel and delete it.

like image 30
Algar Avatar answered Sep 23 '22 20:09

Algar