Retrofit call in Kotlin Coroutines viewModelScope

suspend functions are always safe to call from the main thread. But if you are going to do heavy operations like fetch data from server, database, content loader... it is better to use the appropriate dispatcher, for example, Dispatcher.IO.

If you want you can use this dispatcher with the viewmodelScope, and then all the suspends functions runs with this dispatcher.

Now the ViewModel is an Executor (a class that changes the execute from Main to background)

If you are going to change the dispatcher in the ViewModel, a good practice is to inject this Dispatcher by constructor to test the ViewModel.


It works because Retrofit's suspend implementation delegates to Call<T>.enqueue. This means it already executes on its own background executor by default instead of using the caller's Dispatcher.