NonCancellable
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
}WARNING: This object is not designed to be used with launch, async, and other coroutine builders. if you write launch(NonCancellable) { ... } then not only the newly launched job will not be cancelled when the parent is cancelled, the whole parent-child relation between parent and child is severed. The parent will not wait for the child's completion, nor will be cancelled when the child crashed.
Extensions
Cancels Job of this context with an optional cancellation cause. See Job.cancel for details.
Cancels the job and suspends the invoking coroutine until the cancelled job is complete.
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.
Cancels all children jobs of this coroutine using Job.cancel for all of them with an optional cancellation cause. Unlike Job.cancel on this job as a whole, the state of this job itself is not affected.
Ensures that job in the current context is active.
Ensures that current job is active. If the job is no longer active, throws CancellationException. If the job was cancelled, thrown exception contains the original cancellation cause.
Returns true when the Job of the coroutine in this context is still active (has not completed and was not cancelled yet).
Retrieves the current Job instance from the given CoroutineContext or throws IllegalStateException if no job is present in the context.