Android WorkManager: The Future Is Coming

JetRuby Agency
JetRuby Agency
Published in
6 min readJul 26, 2018

--

When it comes to mobile app development, we stick to the rule: “User goals come first.”

Nevertheless, there are a lot of tasks, which execution should be unnoticeable for users in order to improve the experience (this especially relates to Android development), such as:

  1. Logging
  2. Loading photos and videos
  3. Information synchronization
  4. Calculation

On the other hand, there are various ways of executing them in the background:

  1. Alarm manager
  2. Async task
  3. Threads
  4. Executors
  5. Handlers and loopers
  6. Jobs
  7. Loaders and so on

However, there are more restrictions on the background processes in the latest version of Android. This has to do with the fact that developers want to increase the battery life and improve the custom control to the smartphone processes.

At first glance, this can be the right strategy. However, when it comes to “invisible execution,” everything can change. In this article, we’ll discuss how to save both a good performance and user-friendly UX.

In order to find the best possible solution for background processes, we need to find the answers to the following questions:

  • Should this task be executed right now?
  • Can the execution be delayed indefinitely?
  • Can the task become irrelevant?
  • Should it be executed within the specific period of time?

Nevertheless, we found a formula that can help forget about this challenge. Before you find out the secret, make sure nobody can hear or see you.

Are you ready? So the winning formula is … WorkManager.

The WorkManager API allows specifying deferrable, synchronous tasks and when they should run in an easy way! Using these APIs, you’ll be able to create a task and hand it off to WorkManager to run immediately or at a specified time.

Here are the main features of this technology:

  1. Delayed task execution;
  2. Compliance with the limit values of various Android versions platforms;
  3. Backward compatibility with or without Google play Services;
  4. The possibility to adjust to various conditions. For example, you can make the process start only after establishing an Internet connection.
  5. Create as many sequences as you want.
  6. You can check the state of your app in Run time.

The main classes:

  1. Worker. This is the class that does the whole job. Here will be all the business logic.
  2. WorkRequest. This is a generic class that takes a Worker as:
  3. OneTimeWorkRequest
  4. PeriodicWorkRequest

An example of using Worker:

In order to create your own worker, you need to inherit from worker. Since doWork() method will be executed in a background thread, you won’t have to do anything else. Here’s the way how you can use your Worker:

If your work is connected with some condition, you need to use Constraints:

You can observe the working process and react accordingly:

Work Status is an object that displays the status of work execution:

Functional sequences can be created using methods beginWith() и then().

Concurrent processing is executed the following way:

By the way, concurrent processing can be combined with a cascade one.

Inputs and Outputs.

All output and input data for WorkManager should be named as “Data.” Data is serialized by the work manager map of key value.

We can get all the data from inside the class. Later, we can return the result.

Why do we need to process data to outputData? The answer is simple: For the further work. Here’s a rule that should to be taken into account:

That’s why you need to get previous work results in a new Worker:

What if there is a huge amount of results and the work was parallel? In this case, we’ll need to continue with the process by combining the results.

This challenge can be solved by two sample standard methods:

The first one predetermines the values from the last Data object if they coincide with the previous one.

Having created an array, we’ll get an object with all the keys filling them with values from all data objects. Remember that type conflicts will throw an exception.

Here’s how you can use InputMerger:

In this example, you’ll get a sheet in the format “word — number.” The next step is to combine all the values (if words from books are identical). After this, you’ll be able to find top 10 the most used words.

Canceling Work

It’s worth to mention that id work of the process won’t help you as it works on the principle of auto generation. That’s why you’ll be able to identify the process using Tags:

In a system with more than one user, you’ll be able to understand which process belongs to which user. Moreover, you’ll be able to finish the process using tags.

Use case — SYNC

Here we can see an idea of a unique work without dual processing. When starting, you may specify KEEP. If such work already exists, remain it. The second way is to specify REPLACE. Basically, you’ll need to replace the current work by the work that is being suggested right now. The last way is: APPEND — add this work to the end of a queue. It’s important to notice that such work will start only if all other processes in a chain were successfully implemented.

Use case — PERIODIC WORK

The Bottom Line

In our company, we also try to keep up with all the latest development in the field. We love new technologies and are always on the lookout for new effective solutions.

This time our attention was drawn to Work Manager. Despite the fact that it’s in alpha right now, this tool is highly promising and effective. It already allows us to save much time and effort. In the future, it should provide even more effective solution in terms of executing the delayed tasks.

And what about you guys? How do you keep pace with the progress? Do you know some cool tools/guides? If so, please, don’t hide them from us :)

--

--

JetRuby is a Digital Agency that doesn’t stop moving. We expound on subjects as varied as developing a mobile app and through to disruptive technologies.