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.

Send progress updates from a background worker

Send progress updates from a background worker

- [Instructor] As I showed previously, the WorkManager API lets you send messages from the Worker to the UI. In addition to sending final results when it's successful, you can also report progress periodically. The syntax for this is pretty much the same as sending data but you have to change the Worker class's superclass so that you're using a Coroutine. I'll change the superclass for MyWorker class, from Worker to CoroutineWorker. Now, I don't need to set up a scope that's done automatically. But because I'm now working within a Coroutine, the dowork function has to be suspending, so I'll add the suspend keyword right there. Next, I'll add some code so that I'm mimicking a long running function. In order to set a progress update, call the function setProgress and then pass in a data object. Just like before, I'm going to be using the workDataOf function and sending a pair with a key and a value. So I'm going to…

Contents