Using coroutines scoped to ViewModel for IO

Jumping a little bit late to the party, but here are my 2 cents:
In our code all of the view models inherit from a base view model who implements CoroutineScope
Some of the view models are only there to get a nice proccessed data to the UI, smoe are there to actualy manipulate data and do actual work.

In a typical view model class,
whenever there is some data to be retrieved, we would use this.launch { ... } to get it
whenever there is some work to be done (your example with DB insertions is good here) we use GlobalScope.launch { ... }

1 Like