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.

Solution: Run multiple coroutines in a ViewModel

Solution: Run multiple coroutines in a ViewModel

(upbeat music) - [Instructor] For the current challenge, I asked you to convert an application so instead of using threads and handlers, it was using coroutines. And I asked you to wrap those coroutines inside a view model. Here's my finished version of the application. First of all, in the build.gradl file, I've added the required dependency so I could use view models. Then, I created my view model class. In order to share data with the activity, I created a live data object. It's mutable live data, because it's going to change, and that was wrapped around a pair object containing two integer values. Then I created a function, which is public in Kotlin by default. I looped through the die indices from zero to five. I no longer have direct access to that array, so I couldn't use dice.indices any more. And then each time through the loop, I launched a coroutine. So this means I have six coroutines running…

Contents