What is the difference between Dispatchers.Main and Dispatchers.Default in Kotlin coroutines?
I used viewModelScope.launch {}, which by default executes on the UI thread. This was confusing, as I thought I had to use Dispatchers.Main to perform operations on the UI thread.
On Android, does Dispatchers.Default default to Dispatchers.Main? Are there any drawbacks to using one or the other, or are they interchangeable? If they are interchangeable on Android, will it affect something if I add support for Kotlin multiplatform in the future?
Dispatchers.Main is a coroutine dispatcher that is confined to the main thread operating with UI objects, while Dispatchers.Default is a coroutine dispatcher that is optimized for CPU-intensive work.
On Android, Dispatchers.Default does not default to Dispatchers.Main. They are not interchangeable, as using Dispatchers.Default for UI operations can cause the app to freeze or crash. It is recommended to use Dispatchers.Main for UI operations and Dispatchers.Default for background operations.
Adding support for Kotlin multiplatform in the future should not affect the usage of Dispatchers.Main and Dispatchers.Default on Android.