MainCoroutineDispatcher

abstract class MainCoroutineDispatcher : CoroutineDispatcher (source)

Base class for special CoroutineDispatcher which is confined to application “Main” or “UI” thread and used for any UI-based activities. Instance of MainDispatcher can be obtained by Dispatchers.Main.

Platform may or may not provide instance of MainDispatcher, see documentation to Dispatchers.Main

Constructors

<init>

MainCoroutineDispatcher()

Base class for special CoroutineDispatcher which is confined to application “Main” or “UI” thread and used for any UI-based activities. Instance of MainDispatcher can be obtained by Dispatchers.Main.

Properties

immediate

abstract val immediate: MainCoroutineDispatcher

Returns dispatcher that executes coroutines immediately when it is already in the right context (e.g. current looper is the same as this handler’s looper) without an additional re-dispatch.

Inherited Functions

dispatch

abstract fun dispatch(
    context: CoroutineContext,
    block: Runnable
): Unit

Dispatches execution of a runnable block onto another thread in the given context.

interceptContinuation

fun <T> interceptContinuation(
    continuation: Continuation<T>
): Continuation<T>

Returns continuation that wraps the original continuation, thus intercepting all resumptions.

isDispatchNeeded

open fun isDispatchNeeded(context: CoroutineContext): Boolean

Returns true if execution shall be dispatched onto another thread. The default behaviour for most dispatchers is to return true.

Extension Properties

isActive

Returns true when the Job of the coroutine in this context is still active (has not completed and was not cancelled yet).

Extension Functions

cancel

fun CoroutineContext.cancel(
    cause: CancellationException? = null
): Unit

Cancels Job of this context with an optional cancellation cause. See Job.cancel for details.

cancelChildren

fun CoroutineContext.cancelChildren(
    cause: CancellationException? = null
): Unit

Cancels all children of the Job in this context, without touching the state of this job itself with an optional cancellation cause. See Job.cancel. It does not do anything if there is no job in the context or it has no children.

ensureActive

fun CoroutineContext.ensureActive(): Unit

Ensures that job in the current context is active. Throws IllegalStateException if the context does not have a job in it.

invoke

operator suspend fun <T> CoroutineDispatcher.invoke(
    block: suspend CoroutineScope.() -> T
): T

Calls the specified suspending block with the given CoroutineDispatcher, suspends until it completes, and returns the result.