Dispatchers.Main schedules coroutine work on an application's main or UI thread. Use it for thread-confined operations such as redrawing interface elements in Android, JavaFX, AWT, or Swing applications. section 14.7.1
Switch to the UI thread only when needed
launch(Dispatchers.Default) { // 1 val result = performBackgroundOperation() withContext(Dispatchers.Main) { // 2 updateUI(result) } } The coroutine begins on Dispatchers.Default, performs background work, then withContext switches execution to the main thread for the UI update. A coroutine does not need to run entirely on Dispatchers.Main. section 14.7.3
The actual main dispatcher depends on the configured UI framework because applications do not share one universal definition of the main thread. The framework-specific artifact must be available for this example to work. section 14.7.1 section 14.7.3
