Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement PeriodicWorkRequest chain?

I study Android WorkManager, and fond one problem.

I have 2 Works, first of them fetch some data from server and second preload resources (depends on result of first work). I need doing this chains one time per hour.

I need something like:

workManager.beginWith(work1).then(work2)

But in WorkManger API I found chain only for OneTimeWorkRequest.

like image 876
Andrey Avatar asked Oct 31 '18 21:10

Andrey


People also ask

Can WorkManager be used to repeat jobs?

Given that the repetition interval is in reality a minimum interval, WorkManager makes available an additional parameter that you can use to specify a window where Android can execute your Work.

What is periodic work?

A WorkRequest for repeating work. This work executes multiple times until it is cancelled, with the first execution happening immediately or as soon as the given Constraints are met.


1 Answers

You cannot chain PeriodicWorkRequests. For your use-case you might consider using a OneTimeWorkRequest with a Worker that enqueues a copy of itself at the end of doWork() with an initial delay (to simulate periodicity).

That way you can still do chaining. I would tag all work requests consistently so you can getWorkInfosByTagLiveData() correctly.

like image 143
Rahul Avatar answered Sep 18 '22 15:09

Rahul