Getting started with WorkManager

Getting started with WorkManager

*WorkManager is an Android Jetpack library that enqueues deferrable background work and guarantees their execution when its constraints are met.*

This is the first blog on the WorkManager series.

In this blog, we will cover -

  • Understanding WorkManager and how does it work internally?

  • When can we use it — what are the recommended scenarios where WorkManager comes into the picture

  • Use cases of WorkManager

  • Why WorkManager — Features of WorkManager

Already aware of these? Move on to the next blog — Scheduling work with WorkManager Scheduling work with WorkManager WorkManager is an Android Jetpack library that enqueues deferrable background work and guarantees their execution when…medium.com

Understanding WorkManager

WorkManager is a unified solution for guaranteed background work that’s deferrable, considering Android’s power-saving features and user’s API level.

Workmanager performs the background work as soon as it can even if the user exits from the app or device restarts.

We have Job Scheduler and Alarm Manager, then why WorkManager?

With Job Scheduler and Alarm Manager, we need to find the device API level and then check which API can handle the particular device level and accordingly use it. The code becomes lengthy and complicated. With WorkManager, we as developers don’t have to worry about it. WorkManager is backward compatible to API level 14 and runs with or without Google Play Servies.

Internally, the WorkManager uses these APIs.

Image source: [Schedule tasks with WorkManager](https://cdn.hashnode.com/res/hashnode/image/upload/v1635707168319/UJv_Xs5wK.html)Image source: Schedule tasks with WorkManager

When can we use it?

WorkManager doesn’t depend on the life of the application. It can start when the app is in background, foreground or is in the foreground and then goes to background.

If a work can be classified as guaranteed execution and deferrable, we can do it with WorkManager.

Talking about WorkManager, we use the term guaranteed and deferrable a lot. Let’s understand what they are -

  • Guaranteed work — The work that needs to be done even if the user exits from the app or device restarts.

Let’s take an example, you want to take a bitmap, extract colour and update the UI with that. is this work requires guarantee? No. Let’s take another example, you want to put filters on an image, compress it and then save. Is this important to be done even when the user leaves the app? Yes. This falls into the category of guaranteed execution.

  • Deferrable work — The work that can wait for some time to happen. The one that doesn’t require to be done in an exactly specified time.

Let’s take an example, the user requested to download something. should this be done at a later point when the device has enough battery? No, this is on user’s request. But, let’s say we want to upload logs of the app to the server at 2 AM. Can this wait? Yes. This will not affect the user if not done at the specified time. This work falls into the category of preferable.

Image source: [How to use WorkManager](https://cdn.hashnode.com/res/hashnode/image/upload/v1635707172113/Y2Gnzr-XQ.html)Image source: How to use WorkManager

Use cases

  • Back up application’s data to the server.

  • Apply filters and save images

  • Upload analytics and application logs

Here, we don’t expect the work to be done immediately. It can be performed a little later considering the battery of the device, network constraints etc. The work is required to be executed no matter the app process is live or not or app restarts. The execution is guaranteed.

Why WorkManager?

  • Deals with compatibility issues of different API levels

  • Considers device health for task execution

  • We can add network constraints

  • We can schedule single time tasks, periodic tasks, the chain of tasks — parallel and sequentially

  • Flexible retry policies

  • Work execution guaranteed

Summary

If there is some task that needs to be done by your application, I strongly recommend to first look at what kind of work it is. Check if it is the best effort work or a guaranteed execution work, check if it needs to be done at an exact time or is it deferrable? Use WorkManager for background tasks that require guaranteed execution and is deferrable only.

Thank you for using your precious time in reading this blog. Clap👏 if you found it useful and help others find this article.

This is my first blog post. Show some claps.👏👏