WorkManager in Android & It's Impact on Background Tasks

Impact of WorkManager in Android Background Tasks

WorkManager in Android is a background processing library that is used to conduct background tasks that must be completed in a certain amount of time but not necessarily instantly. We can enqueue our background processing using WorkManager even when the app is not running and the device is reset for whatever reason.

Android application developers consistently seek ways to improve app performance to enhance user experience and engagement. Heavy operations in the main thread lead to poor performance. Hence, processing the data in the background has become a principal part of building a responsive application.

Types of background works

Execution of Background Work

We should execute persistent and impersistent work differently.

Long-running and deferrable impersistent works:

We should not use Long-running and deferrable impersistent works. Instead, we should execute them using WorkManager through persistent work.

1. Immediate Work

Immediate work encompasses the tasks which need to be executed right away. These are crucial tasks for the users to complete immediately and get the result, or they cannot schedule for deferred execution at a later time. They might also be crucial to remain scheduled after the app closes or the device restarts.

Recommended Solution

We should use WorkManager with an OneTimeWorkRequest for persistent immediate works. We can expedite the workRequest with setExpedited().

Example

A chat app creates a Worker to send messages and enqueues the task as a Worker.

2. Long-running Work

We will consider a task as Long-running work if it takes more than 10 minutes.

Recommended Solution

WorkManager helps us handle such tasks using a long-running worker.

Example

An app needs to download a bigger file, where we cannot chunk the workload. We can create a Long-running Worker and enqueue the download. So, the app can start downloading and continue to complete the task even if it takes more than 10 minutes.

3. Deferrable Work

Any task which does not need to execute right away is Deferrable work.

Recommended Solution

The best way to execute the deferrable work is to schedule it through WorkManager. It helps the task achieve its purpose even when the app closes or the device reboots.

Example

An app needs to sync its data to the server regularly. The user will not trigger this sync, and this should happen when the device is idle.

4. Alarms

Alarms are not a part of background work. They are special use cases. We should use AlarmManager only when scheduling the exact alarms, such as calendar events or alarm clocks.

5. Foreground Services

WorkManager in Android 12 restricts the launch of the foreground services from the background. We can use the setForeground() from the WorkManager to handle this case, and this allows the WorkManager to handle the foreground service lifecycle and ensure efficiency.

Conclusion

There are many techniques available in Android to perform background tasks. Coroutines and WorkManager in Android provide the best way to execute them. Coroutines are used for immediate impersistent works. On the other hand, WorkManager helps execute long-running, deferrable, scheduled tasks. WorkManager provides options to run a single request or chain of requests, and we can set constraints to be satisfied to run the same.

Leave a Reply

Your email address will not be published. Required fields are marked *

four × 3 =