I'm developing some features that need to run in the background with a UI update like creating a file from the input stream and notifying the user after it's finished
After trying the work manager for this task. It works well, but is it a good option in your opinion?
Note: I am very interested in updating the user interface during the process and as you know async task deprecated
CodePudding user response:
Yes. Initially, WorkManager
was a library for managing defferable background work. Now however, WorkManager
is a recommended solution for any persistent work, that is, the work that must be completed - no matter if the application process is stopped, or even the device is rebooted. From version 2.3.0-alpha01
, WorkManager
provides first-class support for setting and observing intermediate progress for workers. Updatig the user interface is exactly what this feature can be used for.
For me the easiest way to reason about WorkManager is an extension of your app scopes:
Activity
Scope - the works lives untilonDestroy
lifecycle eventViewModel
Scope - the works lives as long as ViewModel. If you navigate away - the work is stoppedApplication
Scope (eg. Coroutine'sGlobalScope
) - the work lives as long as applcation process. If the user ends the app process in task manager or the system kills it - the work is lostWorkManager
- the work survives process death and device reboot. It is persisted with help of Room DB under the hood, so that you can continue the work after your application process was restarted.