Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WorkManager google api: wait 15 minutes for every periodic worker execution?

Is there a way to test a PERIODIC worker from WorkManager Google API without waiting at least 15 minutes for every execution?

I mean, it is a DEBUG app and I'm running it through Android Studio and I don't want to wait such a long time to test my features.

like image 491
Leandro Silva Avatar asked Mar 14 '19 21:03

Leandro Silva


People also ask

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 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.

Does WorkManager work when app is closed?

Android WorkManager is a background processing library which is used to execute background tasks which should run in a guaranteed way but not necessarily immediately. With WorkManager we can enqueue our background processing even when the app is not running and the device is rebooted for some reason.


2 Answers

You can't.

Periodic work has a minimum interval of 15 minutes and it cannot have an initial delay. You can find the proof in the WorkSpec.java class.

 /**
     * Sets the periodic interval for this unit of work.
     *
     * @param intervalDuration The interval in milliseconds
     */
    public void setPeriodic(long intervalDuration) {
        if (intervalDuration < MIN_PERIODIC_INTERVAL_MILLIS) {
            Logger.get().warning(TAG, String.format(
                    "Interval duration lesser than minimum allowed value; Changed to %s",
                    MIN_PERIODIC_INTERVAL_MILLIS));
            intervalDuration = MIN_PERIODIC_INTERVAL_MILLIS;
        }
        setPeriodic(intervalDuration, intervalDuration);
    }

But there are other ways to deal with that.

  1. Write unit tests using work-testing library and ensure that your business logic works as expected.
  2. Use dependency injection approach and provide a OneTimeWorkRequest in debug mode, for example:
interface Scheduler {
    fun schedule()
}

class DebugScheduler {
    fun schedule() {
        WorkManager.getInstance().enqueue(
            OneTimeWorkRequest.Builder(MyWorker::class.java)
                .build()
        )
    }
}

class ProductionScheduler {
    fun schedule() {
        // your actual scheduling logic
    }
}
like image 173
Saeed Masoumi Avatar answered Sep 17 '22 23:09

Saeed Masoumi


For testing purposes, you can use the work-testing library as shown here: https://developer.android.com/topic/libraries/architecture/workmanager/how-to/testing

Specifically, you want to look at how to test periodic work: https://developer.android.com/topic/libraries/architecture/workmanager/how-to/testing#periodic-work

like image 21
SumirKodes Avatar answered Sep 18 '22 23:09

SumirKodes