How to decrease time period of Workmanager Android?
Asked Answered
K

5

5

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.

Khat answered 15/5, 2020 at 3:34 Comment(0)
C
3

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

Charmer answered 25/4, 2021 at 1:4 Comment(0)
S
2

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.

Schuller answered 15/5, 2020 at 6:24 Comment(2)
Thanks for your response, Above code works when there is an update from Frontend right ? If there is any update from server side then how can we get the data from server to Frontend ?Khat
Workmanger runs on its background thread so it's perfectly fine to fetch server data there. How? the way you do that outside workmanger, it's no different. A worker will do any work you put into it.Schuller
V
1

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.

Vida answered 28/8, 2020 at 13:12 Comment(0)
E
0

In the work manager, there is a minimum time interval between two periodic requests is 15 minutes.

Read this doc: https://developer.android.com/reference/kotlin/androidx/work/PeriodicWorkRequest

you can not reduce it in the work manager.

if you want to reduce time in that case you need to use JobScheduler it gives options with specific time intervals to execute your request.

https://developer.android.com/reference/android/app/job/JobScheduler

Este answered 15/5, 2020 at 4:7 Comment(2)
Thanks for your response Mehul, Disadvantages using JobScheduler ?Khat
@Khat There is no disadvantage for JobScheduer. Backward compatible for work manager is up to API 14 Uses JobScheduler on devices with API 23+Este
F
0

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
Fideliafidelio answered 15/5, 2020 at 4:12 Comment(7)
Thanks for your response Kashish, a bit confusing in the last 2 statements which you mentioned. May you please explain clearlyKhat
So you need to send data when somebody clicks a button or something, right?Fideliafidelio
It will be launched instantly if all the constraints are met, else it'll find an appropriate time to do this, but in most cases will start instantly.Fideliafidelio
Basically, If there is some data which need to be sent from server to frontend, this can be done instantly without button click right ?, Is there any time period for this oneTimeWorkRequest ?Khat
TimePeriod is for PeriodicWorkRequest, which means a PeriodicWorkRequest should have an interval of at least 15mins. But you can start many different Periodic requests altogether. But with OneTimeWorkRequest, there's no such condition but then you do need to take care about the existence policies, etc i.e. cancel the previous work if the new one has started and older hasn't finished yet. Please read the docs for such queries. If my answer helped you, please accept it.Fideliafidelio
Thanks for your amazing response, I will accept it, one query, If there is some data which need to be sent from server to frontend, How can this be done instantly without button click ?Khat
Few ways, either keep on checking again and again by querying server from a scheduled job or use Firebase Messaging service (FCM) Send a message from FCM to your app that new data is available, and whenever your app receives that message just query the backend!Fideliafidelio

© 2022 - 2024 — McMap. All rights reserved.