Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the retry strategy/ mechanism for WorkManager's OneTimeWorkRequest

Tags:

I have the following one-time worker.

// Create a Constraints that defines when the task should run Constraints constraints = new Constraints.Builder()         .setRequiredNetworkType(NetworkType.UNMETERED)         .setRequiresBatteryNotLow(true)         // Many other constraints are available, see the         // Constraints.Builder reference         .build();  OneTimeWorkRequest oneTimeWorkRequest =         new OneTimeWorkRequest.Builder(SyncWorker.class)                 .setConstraints(constraints)                 .addTag(SyncWorker.TAG)                 .build(); 

According to https://developer.android.com/topic/libraries/architecture/workmanager

// (Returning RETRY tells WorkManager to try this task again // later; FAILURE says not to try again.) 

I was wondering, if SyncWorker keep returning RETRY, what is the retry strategy of WorkManager? For instance, what is the maximum retry count for WorkManager? The documentation isn't clear on this.

like image 611
Cheok Yan Cheng Avatar asked May 23 '18 10:05

Cheok Yan Cheng


People also ask

Which API do you use to add constraints to a WorkRequest?

To create a set of constraints and associate it with some work, create a Constraints instance using the Contraints. Builder() and assign it to your WorkRequest. Builder() .

What is OneTimeWorkRequest?

androidx.work.OneTimeWorkRequest. A WorkRequest for non-repeating work. OneTimeWorkRequests can be put in simple or complex graphs of work by using methods like WorkManager. beginWith or WorkManager. beginWith .

When would you use a WorkManager?

Use WorkManager for reliable workWorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts. For example: Sending logs or analytics to backend services. Periodically syncing application data with a server.

What is setBackoffCriteria?

The setBackoffCriteria is used for when you need to specify at which rate to retry if the Work fails.


1 Answers

The default is BackoffPolicy.EXPONENTIAL. We only retry when you ask us to RETRY by returning WorkerResult.RETRY or when constraints that were required for your Worker are now unmet. So for e.g. if you required a NETWORK constraint, and now the device lost its active Network connection - then the Worker will be stopped and be automatically retried (when the constraints are met).

For more information look at the docs.

like image 181
Rahul Avatar answered Oct 10 '22 05:10

Rahul