How to decrease time period of Workmanager Android?

Viewed 2616

I have been checking with the other links, stackoverflow to reduce time period of Work manager, but I found below link

How to reduce time of PeriodicWorkManager in WorkManager

Above link says that minimum time is 15 minutes.

Need to send data instantly from mobile to server. Is there any alternative for reducing the time period ?

Please help me on this.Thanks in Advance.

5 Answers

What I managed to did is a sort of hack. I called a method recursively from doWork(). Then checked the time difference every time to make recursion stop before 15 minutes.

   override suspend fun doWork(): Result {
        startTime = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis())

        doSomeWork()

        return Result.success()
    }

   private suspend fun doSomeWork() {
        Log.d("SomeWorker", "-------> working")

        val currentTime = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis())
        val diff = currentTime - startTime
        if (diff < 12L) {
            Thread.sleep(30000) // this is the new time interval
            doSomeWork()
        }
    }

This is the full worker class: https://gist.github.com/WSAyan/7f3a75d05332984e24ad5418f0b1e38b

You can make your Worker with OneTimeWorkRequest, and just before the closing (read returning) of it's dowWork(), make it register itself again. That will go like:

val tenMinuteRequest = OneTimeWorkRequestBuilder<YourWorker>()
        .setInitialDelay(10, TimeUnit.MINUTES)
        .build()
WorkManager.getInstance(applicationContext)
        .enqueue(tenMinuteRequest)
//return Result.Success here or whatever

When you fire this Worker from another class, it will do its work and reschedules itself right after, and here goes the cycle.

If you need to send instantly, you can try OneTimeWorkRequest. Though its execution will also be dependent on the constraints you add or some other cases, but it'll start right after you call it most of the times.

Docs, Refer

Edit:

And then if you want this periodically, You can create a PeriodicWorkRequest and start your OneTimeWorkRequest in it.

eg. Let's say you're having 2 methods, startOneTimeWorker(), startPeriodicWorker()

If you want to sync it once and instantly OneTimeWorker will do the job.

But if you want to sync instantly and then schedule it also, call startOneTimeWorker(), and then startPeriodicWorker(). Where Periodic worker will be calling startOneTimeWorker() in it's doWork()

So for the first time, OneTimeWorkRequest will be called instantly and then it'll be called according to the schedule of PeriodicTimeWorkRequest.

  • Call OneTimeWorkRequest
  • Schedule PeriodicWorkRequest, calling OneTimeWorkRequest in it

If you want to send Data instantly from Mobile to Server one time then use OneTimeWorkRequestBuilder , otherwise use ForeGround Service instead of WorkManager for sending continuous data upload to server.

Both of these strategy will ensure the data upload at server side. And if it is heavy upload work which may take longer than 10 minutes , please use ForeGround Service definitely.

Related