Skip to content

Workmanager

Devrath edited this page Jun 5, 2021 · 15 revisions
Contents
Definition
Work manager as an assistant
Work manager's Arsenal
When to and when not to use work manager
Main classes of manager
Types of work manager requests
Having the unique work manager instance
Cancel the work manager

Definition

  • Work-Manager is a new library from android architecture components
  • It simplifies the work of getting the background tasks done, even if the application gets killed. This is very hard to achieve in the latest android versions using the old android API's.
  • It adheres to power-saving features like doze-mode.
  • It is backward compatible.

Work manager as an assistant

  • Work-Manager is a clever mobile assistant that helps to perform heavy tasks in the background thread.
  • It is in such a way that, we have certain tasks to complete, we hand over the tasks to the assistant stating the conditions in with the task has to be done. And finally, the assistant takes care of the task getting done.
  • For example, You can tell the assistant work-manager to perform a task every one hour, not to start the work until the device has network, `not to start the work until the battery is low.
  • Work-manager continues the work even if the app is in the background or the OS kills the app.
  • Summarizing the work-manager could intelligently use the defined factors and get the work done for you.

Work manager's Arsenal

* The beauty of work manager is that, Under the hood based on the requirement, the work manager decides which API's to execute and get the task done. * So we ask `work-manager` on what need to be done and not worrying on how it will be done

When to and when not to use work manager

  • Tasks that don't need to be executed instantly need to be executed in the guarantee that is best suited for the work manager.
    • Should you need to hand over the task of performing the network request, parsing the retrieved data, and showing inactivity -> No because that can be done in an instant and can be canceled when the application process is removed.
    • What about the task of sending the logs to the server, processing and syncing the data, upload a large no of videos and images, and others -> Yes because these might not have to be done instantly.

Main classes of WorkManager implementation

Classes Names Description
WorkManager This is the main class to enqueue the work requests
Worker This is a sub-class that does the background task
WorkRequest This defines the background task
WorkStatus Running, Enqueued, Finished etc.

Types of work manager requests

Having the unique work manager instance

  • We can have a policy in such a way that there will be a unique instance of work manager
  • We can use a work policy for this
  • We have certain options.
    • Replace - > If there exists a work manager with a unique tag we can replace it with a new one.
    • Append - > If there exists a work manager with a unique tag we can append the existing one with a new one.
    • Keep - > Ignore the new one and keep the already running one keep running.

Cancel the work manager

  • we can listen to work manager changes using live data and if required we can cancel it
  • We can find the work manager instance using below options
    • cancelUniqueWork()
    • cancelAllWorkByTag()
    • cancelWorkById()