withcontext

kotlin coroutines, what is the difference between coroutineScope and withContext

馋奶兔 提交于 2020-08-01 09:57:05
问题 withContext suspend fun <T> withContext( context: CoroutineContext, block: suspend CoroutineScope.() -> T ): T (source) Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result. suspend fun <R> coroutineScope( block: suspend CoroutineScope.() -> R ): R (source) Creates a CoroutineScope and calls the specified suspend block with this scope. The provided scope inherits its coroutineContext from the outer scope, but overrides the

Why does withContext await for the completion of child coroutines

☆樱花仙子☆ 提交于 2019-12-22 05:58:44
问题 The documentation of withContext states Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result. However, the actual behavior is that it awaits on all the child coroutines as well, and doesn't necessarily return the result of the block but instead propagates any exception in the child coroutine. suspend fun main() { try { val result = withContext(coroutineContext) { launch { delay(1000L) throw Exception("launched coroutine broke