TechnologyMay 30, 2018

Google I/O 2018: Session in 5 mins – Easy Background Processing With WorkManager

Fernando Berrios

During the developer keynote at the 2018 Google I/O conference, Google announced Android Jetpack, a set of new tools, technologies, and APIs tailored toward making Android app development easier and faster. Android Jetpack includes the Android Architecture Components which were introduced at last year’s Google I/O and have been adopted and embraced by many top app developers. This year, a new Android Architecture Component called WorkManager was released.

WorkManager promises to make the management of background processing and asynchronous work easier to develop. The aim of the WorkManager component is to provide an opinionated, application-lifecycle-aware, and backwards-compatible API for developers to perform complex asynchronous operations.

There was a 30-minute session dedicated to this brand-new Architecture Component. You can watch the session or continue reading for a summary and highlights from the talk.

using workmanager

WorkManager takes care of handling a lot of the implementation details that can otherwise get complicated when dealing with long-running asynchronous operations that need to survive process death. This component is not recommended for temporary work that does not need to be completed if the app process is killed or restarted.

The work performed by the component is guaranteed to be executed and is deferable; it can be also configured to have constraints. These constraints allow developers to specify when the work should be performed, for example:

  • Only upload data to server when network connectivity is active.

  • Only perform complex image processing when device is charging.

  • Only clean up caches and temporary data when device is idle.

workmanager and background restrictions

WorkManager respects system background restrictions. It takes into account all the various battery optimization measures that different versions of Android provide before executing work. It is smart enough to not wake up devices from sleep.

The component is queryable, which allows for accessing the state of the work (is it running, has it succeeded or failed?). WorkManager allows for parallel work streams (e.g., executing Work A, B, and C concurrently), but it is also chainable, which allows developers to create dependent work streams (e.g., Work A depends on Work B, which in turn depends on Work C).

One of the features that sets this component apart from other libraries (such as Evernote’s Android Job library) is that it is opportunistic. This means WorkManager will start execution of work in-process as soon as the constraints are met. If your app process is up and running it doesn’t need to wait for JobScheduler to batch process the work.

workmanager’s data

WorkManager defines a format for the inputs and outputs that work items can have, which consists of a simple but flexible data structure called Data that can be easily serialized. During chained work streams, the output of one work item can be configured to be the input of the next work item in the chain.

workmanager combining data

WorkManager supports combining Data objects. This is useful in scenarios where you need to execute some parallel work and then use the output of all those work items to do some additional processing before finishing.

The component also supports best-effort cancellation of work that is in progress. This means that it will cancel a work request, but the request might have already finished since work items are asynchronous. For example, a work item responsible for uploading multiple photos to a server might have already finished sending two or three photos before the cancellation stops the remaining photos from uploading.

workmanager’s unique chains of work

WorkManager introduces chains of unique work with support for existing work policies. This means that a developer can specify some unique work and set a policy to keep, cancel and replace, or append to any existing work if it is requested again. For example, a chain of sync operations on application startup or on user log in / log out flows.

get started with workmanager

The WorkManager component is now in alpha and available via Gradle. Get more information on how to use WorkManager.

Are you an app developer excited by the new Google I/O announcements? Apply to Credera today!