in last Seven Peaks Speaks android webinar: How to code effectively with MAD, Fedor talked about Background Processing with Work Manager, where we will learn about the best practices for scheduling one-time and periodic background work with Jetpack Work Manager.
3. Benefits
What’s good about WorkManager?
● Guaranteed. The work is guaranteed to execute.
● Work constraints. Specify best conditions for work. For example:
○ When the device is using Wi-FI.
○ The device is charging.
○ The device is idle.
○ Etc.
● Flexible scheduling. One-time and repeated work:
○ Can be delayed.
○ Minimum period between repeated work - 15 minutes.
● Retry policies. Provide retry policies in case there are some errors during execution.
● Work chaining. Chain multiple works in specific order with specific constraints.
● Works with RxJava and Coroutines.
● Built-in functionality for long work. Run in foreground and show notifications while the worker does the work.
● Integration with Hilt. Easily inject dependencies using only 3 annotations:
○ @HiltWorker, @AssistedInject, @Assisted
5. Disadvantages
What’s not so good?
● Not precise. Repeated work is executed depending on various conditions and can be delayed by some time.
● Fast work doesn’t show notifications. The built-in functionality for notifications automatically removes the notification
once the job is done. If your work is quick there is a big chance the notification will not appear at all (possible to use
regular notification in this case).
11. Result
Types of Result
Result
retry
success
failure
The Work failed. It will not be restarted.
The Work failed. It will be restarted after
sometime.
The Work succeeded. If it’s periodic work,
it will be scheduled for next time.
12. Create Worker
Other types - Coroutine Worker
Extend
CoroutineWorker
Override suspend
doWork() and return
Result
13. Create Worker
Other types - RxWorker
Extend RxWorker
Override createWork()
and return
Single<Result>
14. Types of work requests
OneTimeWorkRequest | PeriodicWorkRequest
16. One time work features
Specific features
● Chaining. Execute various works depending on different constraints.
● Expedited work. In case of a very important work that must be performed, possible to use expedited work. Features:
○ Importance: Expedited work suits tasks which are important to the user or are user-initiated.
○ Speed: Expedited work best fits short tasks that start immediately and complete within a few minutes.
○ Quotas: A system-level quota that limits foreground execution time determines whether an expedited job can
start.
○ Power Management: Power management restrictions ,such as Battery Saver and Doze, are less likely to affect
expedited work.
○ Latency: The system immediately executes expedited work, provided that the system's current workload
enables it to do so. This means they are latency sensitive and can't be scheduled for later execution.
26. Out of quotas?!
Choose what to in case there are no more quotas for your expedited work
OutOfQuotaPolicy
DROP_WORK_REQUEST
RUN_AS_NON_EXPEDITED_WORK_REQUEST
Run like non-expedited
Cancel the work
28. Periodic work features
Specific features
● Additional parameters for the builder:
○ repeatInterval – The repeat interval in repeatIntervalTimeUnit units
○ repeatIntervalTimeUnit – The TimeUnit for repeatInterval
● Handling existing work with ExistingPeriodicWorkPolicy.
○ REPLACE - If there is existing pending (uncompleted) work with the same unique name, cancel and delete it.
Then, insert the newly-specified work.
○ KEEP - If there is existing pending (uncompleted) work with the same unique name, do nothing. Otherwise,
insert the newly-specified work.
34. Schedule work
Set delay and period time for your request
● One time work. For one time work you can specify initial delay:
○ .setInitialDelay(15L, TimeUnit.SECONDS)
● Periodic work. For periodic work you can specify initial delay and interval:
○ .setInitialDelay(15L, TimeUnit.SECONDS)
○ PeriodicWorkRequest.Builder(MyWorker::class.java,15L, TimeUnit.MINUTES)
38. Pass data between your Work
Using Data
Note: You can pass only primitive types!
39. Pass data between your Work
Using InputMergers
InputMergers
ArrayCreatingInputMerger
OverwritingInputMerger
In case output from two works has same
name the last work will override the
value!
Works that have same output will be
stored in an array!
40. Pass data to the next Work
Pass output in our
Result.success(...)
43. Notify user using ForegroundInfo
setForeground(...) makes the CoroutineWorker run in the context of a foreground android.app.Service
Call setForeground(...)
and pass
ForegroundInfo with
notification ID and
notification. Worker will
take care of the rest!
Note: If the Work is too fast there will be no notification! You can use regular way of showing notification in this case.
48. Hilt set-up
In 3 steps
● Set-up Manifest.
● Set-up Application class.
● Use the following annotations to inject dependencies:
○ @HiltWorker - in Worker class.
○ @AssistedInject - instead of regular @Inject.
○ @Assisted - to inject Application Context and WorkerParameters
● Ready!
50. Application class set-up
Extend Configuration.Provider and override getWorkManagerConfiguration()
Implement interface
Configuration.Provider
Inject
HiltWorkerFactory
Override
getWorkManagerConfi
guration() and set
HiltWorkerFactory