currentCoroutineContext
Returns the current CoroutineContext retrieved by using kotlin.coroutines.coroutineContext. This function is an alias to avoid name clash with CoroutineScope.coroutineContext:
// ANTIPATTERN! DO NOT WRITE SUCH A CODE
suspend fun CoroutineScope.suspendFunWithScope() {
// Name of the CoroutineScope.coroutineContext in 'this' position, same as `this.coroutineContext`
println(coroutineContext[CoroutineName])
// Name of the context that invoked this suspend function, same as `kotlin.coroutines.coroutineContext`
println(currentCoroutineContext()[CoroutineName])
}
withContext(CoroutineName("Caller")) {
// Will print 'CoroutineName("Receiver")' and 'CoroutineName("Caller")'
CoroutineScope("Receiver").suspendFunWithScope()
}
Content copied to clipboard
This function should always be preferred over kotlin.coroutines.coroutineContext property even when there is no explicit clash.