public class CoroutineScopeKt
Modifier and Type | Method and Description |
---|---|
static CoroutineScope |
CoroutineScope(NonExistentClass context)
Creates
interface CoroutineScope that wraps the given coroutine context. |
static <R> java.lang.Object |
coroutineScope(kotlin.jvm.functions.Function2<? super kotlinx.coroutines.CoroutineScope,? super kotlin.coroutines.experimental.Continuation<? super R>,? extends java.lang.Object> block,
kotlin.coroutines.experimental.Continuation<? super R> p)
Creates new
interface CoroutineScope and calls the specified suspend block with this scope.
The provided scope inherits its CoroutineScope.getCoroutineContext from the outer scope, but overrides
context's interface Job . |
static boolean |
isActive(CoroutineScope $receiver)
Returns
true when current interface Job is still active (has not completed and was not cancelled yet). |
static CoroutineScope |
plus(CoroutineScope $receiver,
NonExistentClass context)
Adds the specified coroutine context to this scope, overriding existing elements in the current
scope's context with the corresponding keys.
|
public static CoroutineScope plus(CoroutineScope $receiver, NonExistentClass context)
Adds the specified coroutine context to this scope, overriding existing elements in the current scope's context with the corresponding keys.
This is a shorthand for CoroutineScope(thisScope + context)
.
public static boolean isActive(CoroutineScope $receiver)
Returns true
when current interface Job
is still active (has not completed and was not cancelled yet).
Check this property in long-running computation loops to support cancellation:
while (isActive) {
// do some computation
}
This property is a shortcut for coroutineContext.isActive
in the scope when
interface CoroutineScope
is available.
See coroutineContext,
CoroutineScopeKt.isActive
and Job.isActive
.
public static <R> java.lang.Object coroutineScope(kotlin.jvm.functions.Function2<? super kotlinx.coroutines.CoroutineScope,? super kotlin.coroutines.experimental.Continuation<? super R>,? extends java.lang.Object> block, kotlin.coroutines.experimental.Continuation<? super R> p)
Creates new interface CoroutineScope
and calls the specified suspend block with this scope.
The provided scope inherits its CoroutineScope.getCoroutineContext
from the outer scope, but overrides
context's interface Job
.
This function is designed for a parallel decomposition of work. When any child coroutine in this scope fails,
this scope fails and all the rest of the children are cancelled (for a different behavior see SupervisorKt.supervisorScope
).
This function returns as soon as given block and all its children coroutines are completed.
Example of the scope usages looks like this:
suspend fun loadDataForUI() = coroutineScope {
val data = async { // <- extension on current scope
... load some UI data ...
}
withContext(UI) {
doSomeWork()
val result = data.await()
display(result)
}
}
Semantics of the scope in this example:
loadDataForUI
returns as soon as data is loaded and UI is updated.
If doSomeWork
throws an exception, then async
task is cancelled and loadDataForUI
rethrows that exception.
If outer scope of loadDataForUI
is cancelled, both started async
and withContext
are cancelled.
Method may throw CancellationException if the current job was cancelled externally
or may throw the corresponding unhandled Throwable if there is any unhandled exception in this scope
(for example, from a crashed coroutine that was started with BuildersKt.launch
in this scope).
public static CoroutineScope CoroutineScope(NonExistentClass context)
Creates interface CoroutineScope
that wraps the given coroutine context.
If the given context does not contain a interface Job
element, then a default Job()
is created.
This way, cancellation or failure or any child coroutine in this scope cancels all the other children,
just like inside CoroutineScopeKt.coroutineScope
block.