[MUSIC PLAYING]
SPEAKER: In the previous video you
learned about the advantages of using WorkManager.
In this video, you will see the code needed
to create and enqueue a task.
Let's take a look at three important classes
that you need to know to use WorkManager in your app--
Worker, WorkRequest, and WorkManager.
Let's begin with the Worker class.
Your Worker implementations need to extend the Worker class
and overwrite its doWork method to define what your task does.
WorkManager uses this function to execute your task
asynchronously in the background thread.
Next, you need to create a WorkRequest that represents
a request to perform the task.
The WorkRequest also allows you to specify constraints
over this task, such as run this task only
while the phone is charging or when the device is connected
to Wi-Fi.
You can also use WorkRequests to specify the frequency
that you want to run your tasks, such as once or periodically.
Finally, the WorkManager class takes all the work constraints
into consideration and runs your task
when these conditions are met.
WorkManager also allows you to create
a chain of multiple workers.
This functionality is particularly handy
when you want to run several tasks simultaneously
or in a particular order.
Let's say you want to apply multiple filters
on a particular image.
When it's complete, compress the image.
And finally, upload the compressed image to a server.
If you translate this to a WorkManager worker tree,
the app needs to queue three image filter
workers to run in parallel.
The output of these workers are then
passed to a compress worker.
Once the compress worker completes,
the output is passed into the upload worker, which uploads
the image to a remote server.
Notice there is battery constraint on the filter image
workers, storage constraint on the compress worker,
and network constraint on the upload worker.
WorkManager will check if these constraints
are met before running the associated worker.
Now let's see how this translates into code.
Your code will look something like this
once you create the workers and WorkRequests with the desired
constraints.
To start queuing your workers, get an instance of WorkManager.
Call beginWith function, passing in the imageFilter
workers as a list so they will be executed in parallel.
Next, for each worker, call then function
and pass in the worker that needs to run sequentially.
Finally, make a call to enqueue to schedule your tasks.
Note, if any worker creates and returns an output,
the WorkManager will pass the output
as the input of the next worker in the chain.
WorkManager is the recommended solution for all tasks
that need to run longer than the user remains
on a single screen.
Whether the user navigates away from that screen,
the application or even the device restarts,
WorkManager guarantees that your tasks will be executed.
That's not all.
With WorkManager, you can do a lot more, such as observing
the work request status, and have backoff policies
for retiring work.
You can also define periodic WorkRequests
which can run a particular worker repeatedly.
You can use tags to tag a particular WorkRequest.
Tags can be used to observe the WorkRequest status
or cancel the request if it is no longer needed.
To learn more about features, tips, and techniques
to use WorkManager, please check out the codelab, articles,
and documentation linked below.