NonCancellable

object NonCancellable : 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
}

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).

job

Retrieves the current Job instance from the given CoroutineContext or throws IllegalStateException if no job is present in the context.

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.

cancel

fun Job.cancel(
    message: String,
    cause: Throwable? = null
): Unit

Cancels current job, including all its children with a specified diagnostic error message. A cause can be specified to provide additional details on a cancellation reason for debugging purposes.

cancelAndJoin

suspend fun Job.cancelAndJoin(): Unit

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