NonCancellable

object NonCancellable : AbstractCoroutineContextElement, Job (source)

A non-cancelable job that is always active. It is designed for withContext function to prevent cancellation of code blocks that need to be executed without cancellation.

Use it like this:

withContext(NonCancellable) {
    // this code will not be cancelled
}

Properties

children

val children: Sequence<Job>

Always returns emptySequence.

isActive

val isActive: Boolean

Always returns true.

isCancelled

val isCancelled: Boolean

Always returns false.

isCompleted

val isCompleted: Boolean

Always returns false.

onJoin

val onJoin: SelectClause0

Clause for select expression of join suspending function that selects when the job is complete. This clause never fails, even if the job completes exceptionally.

Functions

attachChild

fun attachChild(child: Job): DisposableHandle

Always returns NonDisposableHandle and does not do anything.

cancel

fun cancel(cause: Throwable?): Boolean

Always returns false.

cancelChildren

fun cancelChildren(cause: Throwable?): Unit

Does not do anything.

getCancellationException

fun getCancellationException(): CancellationException

Always throws IllegalStateException.

invokeOnCompletion

fun invokeOnCompletion(
    handler: CompletionHandler
): DisposableHandle
fun invokeOnCompletion(
    handler: CompletionHandler,
    onCancelling: Boolean
): DisposableHandle
fun invokeOnCompletion(
    onCancelling_: Boolean,
    handler: CompletionHandler
): DisposableHandle
fun invokeOnCompletion(
    onCancelling: Boolean,
    invokeImmediately: Boolean,
    handler: CompletionHandler
): DisposableHandle

Always returns NonDisposableHandle.

join

suspend fun join(): Unit

Always throws UnsupportedOperationException.

start

fun start(): Boolean

Always returns false.

Extension Functions

cancel

fun CoroutineContext.cancel(
    cause: Throwable? = null
): Boolean

Cancels Job of this context with an optional cancellation cause. The result is true if the job was cancelled as a result of this invocation and false if there is no job in the context or if it was already cancelled or completed. See Job.cancel for details.

cancelAndJoin

suspend fun Job.cancelAndJoin(): Unit

Cancels the job and suspends invoking coroutine until the cancelled job is complete.

cancelFutureOnCompletion

fun Job.cancelFutureOnCompletion(
    future: Future<*>
): DisposableHandle

Cancels a specified future when this job is complete.

disposeOnCompletion

fun Job.disposeOnCompletion(
    handle: DisposableHandle
): DisposableHandle

Disposes a specified handle when this job is complete.

joinChildren

suspend fun Job.joinChildren(): Unit

Suspends coroutine until all children of this job are complete using Job.join for all of them. Unlike Job.join on this job as a whole, it does not wait until this job is complete.