I'm launching a coroutine in a view model to do some IO operation such as api call and database queries. I know there are few ways it can be done that's why it's confusing to me which is the right approach.
What's the difference between the two and which is the proper approach?
fun loadProducts() {
viewModelScope.launch {
val productsDeferred = async(Dispatchers.IO) { downloadProducts() }
this.products = productsDeferred.await()
}
}
fun loadProducts2() {
viewModelScope.launch {
this.products = withContext(Dispatchers.IO) { downloadProducts() }
}
}
In this example I'm using a flow to load products from roomdb. I wanted the changes in db to reflect to the UI that's why I used a flow. Do I need to switch context to Dispatchers.IO when calling loadProductsFromDatabaseAsFlow and then switch to Main in the collect block? Again, what's the proper way to do that?
fun loadProducts3() {
viewModelScope.launch {
loadProductsFromDatabaseAsFlow().collect {
this.products = it
}
}
}