From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Get results from a background worker

Get results from a background worker

- [Instructor] When you launch a WorkRequest with WorkManager, it runs asynchronously. You can observe the worker though and respond either while it's running or when its work is complete. The worker object provides a live data object. It's exactly the same kind of live data object you might work with in a view model. The live data object can publish information, and your user interface can observe it and react. First I'll set up code to be notified when a task has completed successfully. I'm going to be using the WorkManager object more than once, so I'll change this code a bit so I'm declaring it and I have a single variable to work with. Now I'll add code to observe the request. I'll start with workManager, then .getWorkInfoByIdLiveData. That's a long function name, but essentially you're getting a reference to the work information by referencing the ID of the request. And I'll pass that value in right here with…

Contents