androidandroid-workmanager

What are differences between BackoffPolicy.EXPONENTIAL and BackoffPolicy.LINEAR when working with Work Manager?


There is no any official doc (as I have read the docs at least) that explain the usage and the mechanism behind these two modes . How do they work ? And what problem do they solve ?

I will appreciate that if anyone can simplified it for me , because I have tested both and have not seen any interesting thing . If you ask me , I would say that OneTimeWorkRequest.setBackoffCriteria() does not affect to the work .

Here are my codes ,

@Override
public void doSomethingUseful(String order) {

    Constraints constraint = new Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .build();

    Data data = new Data.Builder()
            .putString("order", order)
            .build();

    OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(OrderSenderWorker.class)
            .setConstraints(constraint)
            .setInputData(data)
            .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 15, TimeUnit.SECONDS)
            .build();

    WorkManager.getInstance().beginUniqueWork("refresh-order", ExistingWorkPolicy.REPLACE, oneTimeWorkRequest).enqueue();

}

And in Worker class whenever I get something wrong , I do return WorkerResult.RETRY in doWork() method.

Thanks in advance .


Solution

  • Taking into account that the WorkManager uses the run attempt count as reference, for a BackoffPolicy of 15 seconds, will be as next:

    The work start time, is when the work was first executed (the 1st run attempt).

    Run attempt count is how many times the WorkManager has tried to execute an specific Work.

    Also note that the maximum delay will be capped at WorkRequest.MAX_BACKOFF_MILLIS.

    Take into consideration that a retry will only happen if you specify that the Work requires it by returning WorkerResult.RETRY