Main

common
val Main: MainCoroutineDispatcher

A coroutine dispatcher that is confined to the Main thread operating with UI objects. Usually such dispatchers are single-threaded.

Access to this property may throw an IllegalStateException if no main dispatchers are present in the classpath.

Depending on platform and classpath it can be mapped to different dispatchers:

  • On JS and Native it is equivalent to the Default dispatcher.

  • On JVM it either the Android main thread dispatcher, JavaFx or Swing EDT dispatcher. It is chosen by the ServiceLoader.

In order to work with the Main dispatcher, the following artifact should be added to the project runtime dependencies:

  • kotlinx-coroutines-android — for Android Main thread dispatcher

  • kotlinx-coroutines-javafx — for JavaFx Application thread dispatcher

  • kotlinx-coroutines-swing — for Swing EDT dispatcher

Implementation note: MainCoroutineDispatcher.immediate is not supported on the Native and JS platforms.

val Main: MainCoroutineDispatcher
val Main: MainCoroutineDispatcher
val Main: MainCoroutineDispatcher

A coroutine dispatcher that is confined to the Main thread operating with UI objects. This dispatcher can be used either directly or via MainScope factory. Usually such dispatcher is single-threaded.

Access to this property may throw IllegalStateException if no main thread dispatchers are present in the classpath.

Depending on platform and classpath it can be mapped to different dispatchers:

  • On JS and Native it is equivalent of Default dispatcher.

  • On JVM it is either Android main thread dispatcher, JavaFx or Swing EDT dispatcher. It is chosen by ServiceLoader.

In order to work with Main dispatcher, the following artifacts should be added to project runtime dependencies:

  • kotlinx-coroutines-android for Android Main thread dispatcher

  • kotlinx-coroutines-javafx for JavaFx Application thread dispatcher

  • kotlinx-coroutines-swing for Swing EDT dispatcher

In order to set a custom Main dispatcher for testing purposes, add the kotlinx-coroutines-test artifact to project test dependencies.

Implementation note: MainCoroutineDispatcher.immediate is not supported on Native and JS platforms.

Sources

common source
Link copied to clipboard
js source
Link copied to clipboard
jvm source
Link copied to clipboard
native source
Link copied to clipboard