gpt4 book ai didi

android - Kotlin 中 CoroutineScope 和 coroutineScope 的区别

转载 作者:行者123 更新时间:2023-12-02 11:52:47 25 4
gpt4 key购买 nike

任何人都可以明确功能之间的关系CoroutineScope()coroutineScope() ?

当我试图检查源代码时,我发现它们都是CoroutineScope.kt的函数。 .此外,coroutineScope()suspend函数,而另一个是 normal功能

以下是我能找到的文档:

/**
* Creates a [CoroutineScope] that wraps the given coroutine [context].
*
* If the given [context] does not contain a [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 [coroutineScope] block.
*/
@Suppress("FunctionName")
public fun CoroutineScope(context: CoroutineContext): CoroutineScope =
ContextScope(if (context[Job] != null) context else context + Job())


/**
* Creates a [CoroutineScope] and calls the specified suspend block with this scope.
* The provided scope inherits its [coroutineContext][CoroutineScope.coroutineContext] from the outer scope, but overrides
* the context's [Job].
*
* This function is designed for _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 [supervisorScope]).
* This function returns as soon as the given block and all its children coroutines are completed.
* A usage example of a scope looks like this:
*
* ```
* suspend fun showSomeData() = coroutineScope {
*
* val data = async(Dispatchers.IO) { // <- extension on current scope
* ... load some UI data for the Main thread ...
* }
*
* withContext(Dispatchers.Main) {
* doSomeWork()
* val result = data.await()
* display(result)
* }
* }
* ```
*
* The scope in this example has the following semantics:
* 1) `showSomeData` returns as soon as the data is loaded and displayed in the UI.
* 2) If `doSomeWork` throws an exception, then the `async` task is cancelled and `showSomeData` rethrows that exception.
* 3) If the outer scope of `showSomeData` is cancelled, both started `async` and `withContext` blocks are cancelled.
* 4) If the `async` block fails, `withContext` will be cancelled.
*
* The method may throw a [CancellationException] if the current job was cancelled externally
* or may throw a corresponding unhandled [Throwable] if there is any unhandled exception in this scope
* (for example, from a crashed coroutine that was started with [launch][CoroutineScope.launch] in this scope).
*/
public suspend fun <R> coroutineScope(block: suspend CoroutineScope.() -> R): R =
suspendCoroutineUninterceptedOrReturn { uCont ->
val coroutine = ScopeCoroutine(uCont.context, uCont)
coroutine.startUndispatchedOrReturn(coroutine, block)
}

我想弄清楚它们之间的区别。如果有人可以回答何时使用哪一个,那将很有帮助。

最佳答案

CoroutineScope 之间的最佳区别(大写 C 版本)与 coroutineScope (较小的 c 版本),我可以弄清楚并且很容易理解的是将它们与 相关联非结构化 对比 结构化 并发
让我分享一个例子:

class MainActivity : AppCompatActivity() {
private lateinit var btn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

btn = findViewById(R.id.start_btn)
btn.setOnClickListener {
CoroutineScope(Dispatchers.Main).launch {
val result = downloadUserData()
Toast.makeText(applicationContext, "Result : $result", Toast.LENGTH_LONG).show()
}
}
}

private suspend fun downloadUserData(): Int {
var result = 0
// Here, we use CoroutineScope (Capital C version) which will start a new scope and
// launch coroutine in new scope Dispatchers.IO, Not In Parent Scope which is Dispatchers.Main
// Thus, this function would directly return without waiting for loop completion and will return 0
CoroutineScope(Dispatchers.IO).launch {
for (i in 0 until 100) {
kotlinx.coroutines.delay(10)
result++
}
}
return result
}
}
输出 : Result : 0这是 的示例非结构化并发不能保证子协程在返回之前完成。因此,调用者/父协程会得到子协程返回的错误值。甚至,当子协程已经返回时,子协程可能正在后台运行(处于 Activity 状态),这可能导致 内存泄漏 在某些情况下。
解决方案:
当我们需要在多个协程之间进行通信时,我们需要确保 结构化并发 (推荐的)
这可以通过在子/被调用协程内重新使用父/调用者协程范围来完成。这可以通过 coroutineScope {} 来实现(较小的 c)子/被调用协程内的版本。
private suspend fun downloadUserData(): Int {
var result = 0
// By using coroutineScope (Smaller c version) below, we ensure that this coroutine would execute in the
// parent/caller coroutine's scope, so it would make sure that the for loop would complete
// before returning from this suspended function. This will return 20000 properly
coroutineScope {
for (i in 0 until 100) {
kotlinx.coroutines.delay(10)
result++
}
}
return result
}
输出: Result : 100

关于android - Kotlin 中 CoroutineScope 和 coroutineScope 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59368838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com