Chaining Worker With Android WorkManager
Chaining the Worker is the most attractive features of WorkManager. With the work manager, we can easily chain multiple workers together and the run workers in a particular order.
I’ve written some pretty good articles on WorkManager before. In these articles, I briefly explain how to use the work manager, setting input and output data, and setting data with InputMerger.
When to Chain Works:
- Suppose your app wants to run three workers parallel and when these workers complete its execution you want to run another worker.
- You want to compress some files before sending it to the server. For this scenario you want to execute compress worker first and then only you need to send it to the server.
Types of Chaining:
- Sequential Chaining: It is a simple chaining in which first task start execution then only the second task executes. With sequential chaining, the output data from one worker become input to the next worker.
- Parallel Chaining: In this type of chaining we run some parallel task. When this parallel task completes its execution then only the next one start execution. In parallel chaining, the output data from these parallel workers will fall into as input in the next worker.
- Multiple Parallel Chaining: WorkManager gives us the ability to executes multiple chains in parallel. With WorkContinuation class we can combine multiple chains and run them in parallel sequence.
Android App Setup:
So, enough of the chaining theory lets see how we can use them in our Android app. First, add the dependency to the build. gradle file.
implementation 'android.arch.work:work-runtime:$current_version'
Now we add the dependency lets see how to use chaining.
1. Sequential Chaining:
For sequential chaining, we can say that, we want to execute the first task and when it completes then only execute the next task. Below shows the example of sequential chaining.
WorkManager.getInstance() .beginWith(firstWorker) .then(secondWorker) .enqueue()
1.1 beginWith:
The beginWith methods indicate the start of the chain. The beginWith method returns WorkContinuation instance.
1.2 then:
With WorkContinuation instance we can add as many then as we want. It’s like the stream of worker executing in the given order.
Note: If the firstWorker failed due to some reason, then the chain won’t run all the next workers in the chain. So in our case, if firstWorker failed then the secondWorker will not run.
For to understand the data flow between these workers see my previous article in which I briefly explain how we can send data from one worker to another worker.
2. Parallel Chaining:
Like I said before with parallel chaining you can execute multiple workers simultaneously and when these workers complete its execution then the next worker start its execution. Below shows the example of parallel chaining.
WorkManager .getInstance() .beginWith(firstWork,secondWork,thirdWork) .then(fourthWorker) .then(fifthWorker) .enqueue()
2.1 beginWith:
In here beginWith method starts execution of three workers in parallel. When these workers complete its execution then only the fourth worker starts its execution and then the fifth worker.
2.2 then:
Like I said above the then method will start if the previous worker completes its execution.
The same reason applies here if some worker fails due to some error then the next worker will not starts. In parallel chaining, if the keys are the same for input and output then the better approach is to use InputMerger, instead of simple override data.
For to understand data flow between the parallel chaining, I wrote a pretty good article on InputMerger. In this article, I briefly explain why we use InputMerger instead of simple override data inputs.
3. Multiple Parallel Chaining:
You may want to run some parallel workers with chaining. Let’s say you have three large files and before sending them to the server you want to compress them as parallel. And when individual worker chain completes the compressing then only send it to the server. For these types of work chaining, we can use the combine method inside WorkContinuation class. Let’s see an example of how we can use the above scenario with work continuation.
val workManager: WorkManager = WorkManager.getInstance() val chain1 = workManager .beginWith(firstCompressFileWorker) .then(uploadFileWorker) val chain2 = workManager .beginWith(secondCompressFileWorker) .then(uploadFileWorker) val chain3 = workManager .beginWith(thirdCompressFileWorker) .then(uploadFileWorker) WorkContinuation.combine(chain1,chain2,chain3) .enqueue()
All of these chain workers will execute in parallel when enqueueing the workers. One thing noticeable here that upload file worker will not run before compress file worker.
Now there are a lot of ways of combining the workers and chaining them with parallel and sequential. With your own need, you can easily define the dependencies of the worker or execute them in parallel.
That’s it, this is my demonstration about how to chain your tasks with the work manager and execute them in parallel. If you’ve any queries regarding this post or about work manager please do comment below.
Thank you for being here and keep reading…
alive all the time between 10pm and 6am.
I’m using WorkManager in a sleep monitor app. I want a worker to be alive between nigth 10pm and morning 6am. For that once worker is started, i’m using a loop with thread.sleep(10 minutes). It is not working as intended.
How long can a worker in work manager be alive ?
Any idea how can i achieve the above functionality ?
Hey PradeepKumar,
You want your worker to be alive at 10pm and morning 6am or you want to alive all of time between 10pm to morning 6am.
alive all the time between 10pm and 6am.
Someone already answer your question here.