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.
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()
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()
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.
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…
Recommended Training – Treehouse
From beginner to advanced, our recommended coding training is Treehouse.
Treehouse is an online training service that teaches web design, web development and app development with videos, quizzes and interactive coding exercises.
Treehouse's mission is to bring technology education to those who can't get it, and is committed to helping its students find jobs. If you're looking to turn coding into your career, you should consider Treehouse.
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, we will receive an affiliate commission. Regardless, we only recommend products or services we use personally and believe will add value to our readers.