Doing background work with Work Manager - Part 1

WorkManager is part of the Android Jetpack Library that makes it easy to schedule asynchronous but deferrable tasks that are expected to run when the app is open or closed or even when the device restarts. It was built as a perfect replacement for all previous Android background job scheduling APIs like the JobScheduler. It integrates modern features that are backward compatible to API level 14 (Android 4.0).

This article is divided into two parts, in part one, we will check out the features of the WorkManager and when to choose WorkManager as a method for handling background tasks. In part two, we will cover how to use WorkManager to schedule deferred work.

Features of WorkManager

Below are the features of WorkManager that makes it suitable for deferrable work:

Powerful Work Scheduling

WorkManager does a great job of managing background tasks that are expected to be done once or repeatedly. Background work can be performed in a parallel or sequential manner, giving room for specifying the order of execution.

Work Constraints

You may want to perform a task only when a condition is met (For instance, when Wi-Fi is enabled). Using Work Constraints, you can specify these conditions declaratively before WorkManager executes the task.

Retrying Work

With WorkManager, you can specify work retry strategies when your work is stopped or when you may want to perform the same task at some later time.

OS Compatibility

WorkManager includes features that are compatible to API level 14 (Android 4.0), taking care of compatibility issues that may arise while incorporating best practices for battery and system health.

Chained Work

Chaining work that may run over a period of time is possible with WorkManager for complex or related work.

When to use WorkManager

A common misconception about WorkManager is that it is used for any task that needs to run in the background. While WorkManager can be used to achieve this, its main purpose is to execute jobs that are expected to run sometime in the future. There are other solutions that may be employed for this type of use case such as ThreadPools, Kotlin Coroutines, or using libraries like RxJava.

For WorkManager, it is best used for any background work that meets the following conditions:

  • Task must be deferrable: A task is deferrable if it is run sometime in the future. For example, a news app that wants to get the latest news from a remote server when internet connection is enabled and send notifications to the user can use WorkManager. WorkMananger manages the task in a battery-efficient way while respecting OS restrictions.

  • Task needs to be finished: In the above scenario, when fetching the latest news, this kind of task needs to be completed in order to feed notifications to the user, even if the network connection is poor or the device restarts, WorkManager guarantees that the task finishes.

Conclusion

To summarize, WorkManager does a great job of managing deferrable asynchronous tasks in a clear and concise manner. As we will see in part two of this article, scheduling a task with WorkManager only requires us to write few lines of code in an efficient way that is compatible with many Android API levels.

Resources

JPRGo9sO7.gif

Thank you for reading, if you found it useful, do not hesitate to like and share as others may find it useful as well. Also, anticipate the second part of this article where we will be scheduling our first work with WorkManager.

If you have questions or feedback for me, kindly drop them in the comments or send me a direct message on Twitter.