6rism0
6rism0

Reputation: 133

Does the WorkManager executes its PeriodicWork immediately after all constraints are met?

It seems like I can't find any specific information about how the WorkManager handles its work after all constrains are met.

Let's say we've set up a WorkManager like this.

val constraints = Constraints.Builder()
      .setRequiredNetworkType(NetworkType.CONNECTED)
      .build()

val syncOnlyOnce = PeriodicWorkRequest.Builder(PeriodicJob::class.java, updateInterval, TimeUnit.MILLISECONDS)
     .setConstraints(constraints)
     .build()

Let's say the updateInterval is 1h, network is connected and the work is executed. One hour has passed, the device is disconnected, the work is not executed.

What happens when the device reconnects? Does the WorkManager execute its work immediately (respectively the next possible execution window), or does it need to wait another period (e.g. ~1h)?

Upvotes: 1

Views: 695

Answers (2)

SumirKodes
SumirKodes

Reputation: 563

No, WorkManager does not necessarily execute your work immediately. As noted in the official documentation (https://developer.android.com/topic/libraries/architecture/workmanager), work is deferrable. In situations like device doze mode, your work may not get executed right away.

Please see this page if you want to know more about your options: https://developer.android.com/guide/background/

Upvotes: 3

apksherlock
apksherlock

Reputation: 8371

Let's say the updateInterval is 1h, network is connected and the work is executed. One hour has passed, the device is disconnected, the work is not executed.

Android system makes sure that on the first moment the constraints meet, your WorkManager will start execution. You have to trust the Android team for this but honestly looks well tested. It also happens when you reboot the device, the WorkManager just "magically" reappears.

You will have certain moments when Android could kill your background worker though, but that's pretty rare. It could happen for example when you force stop your app from settings.

I think I have had some seconds delay for this case, but happens only on my OnePlus device.

Upvotes: 0

Related Questions