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
From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines
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
-
-
-
-
-
-
(Locked)
Run background tasks with IntentService5m 22s
-
(Locked)
Upgrade IntentService to JobIntentService4m 37s
-
(Locked)
Send results from JobIntentService to the UI7m 25s
-
(Locked)
Manage deferrable tasks with WorkManager5m 35s
-
(Locked)
Set work request constraints3m 9s
-
(Locked)
Get results from a background worker4m 56s
-
(Locked)
Send progress updates from a background worker5m 2s
-
(Locked)
-
-