Kotlin 中 CoroutineScope 和 coroutineScope 的区别

Kus*_*hal 11 android suspend kotlin kotlin-coroutines coroutinescope

任何人都可以明确功能之间的关系 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())
Run Code Online (Sandbox Code Playgroud)

/**
 * 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)
    }
Run Code Online (Sandbox Code Playgroud)

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

Kus*_*hal 18

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
    }
}
Run Code Online (Sandbox Code Playgroud)

输出 : Result : 0

这是一个非结构化并发的例子,它不能保证子协程在返回之前完成。因此,调用者/父协程会得到子协程返回的错误值。甚至,当子协程已经返回时,子协程可能在后台运行(处于活动状态),这在某些情况下可能会导致内存泄漏

解决方案 :

当我们需要在多个协程之间进行通信时,我们需要确保结构化并发(推荐)

这可以通过在子/被调用者协程中重新使用父/调用者协程范围来完成。这可以通过coroutineScope {}child/callee 协程中的(Smaller 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
}
Run Code Online (Sandbox Code Playgroud)

输出 : Result : 100

  • 如果你完全删除“coroutineScope”,效果会不一样吗? (3认同)
  • 为什么它看起来部分像java代码?例如 `int result = 0` 而不是 `var result: Int = 0` 或 `extends` 关键字而不是 `:` 另一方面,您可以看到来自 Kotlin 语言的 `suspend` 关键字。这里发生了什么? (2认同)
  • @Kushal,我将这个半 koitlin 代码修改为 kotlin 代码。如果您保留原帖,请回复。我测试了这段代码,我认为它似乎适合您的目的。 (2认同)
  • @rtsketo是的,我首先想知道为什么我们需要使用像这样的coroutineScope全局函数创建子协程,如果没有它我们会得到相同的结果。 (2认同)

Mar*_*nik 12

CoroutineScope()只不过是CoroutineScope对象的工厂,而CoroutineScope对象只不过是CoroutineContext. 它在协程中没有积极作用,但它是基础设施的重要组成部分,可以轻松正确地进行结构化并发。这是因为所有协程构建器都喜欢launchasync都是扩展函数CoroutineScope并继承其上下文。

您很少(如果有的话)需要调用,CoroutineScope()因为通常您要么选择现有的协程范围,要么由其他便利函数(如MainScope在 Android 上)或 Kotlin 内部为您创建一个范围。

coroutineScope(),另一方面,是一个函数,它执行您在子协程中传递给它的块。它基本上是 for 的别名,withContext(this.coroutineContext)当您想在前台继续一些工作时启动一个或多个后台协程时,您应该主要使用它,然后在完成块时加入后台协程。


Dan*_*ato 8

它们是两个完全不同的东西。

CoroutineScope是定义协程作用域概念的接口:要启动和创建协程,您需要一个协程。

GlobalScope例如,是全局范围的实例。

CoroutineScope()是一个全局函数,它创建一个CoroutineScope

当您有一个范围时,您可以执行launch()async()或与执行协程相关的任何其他方法。

// create a context
val myContext = Dispacher.IO
// you can combine dispachers, parent jobs etc.
// create the new scope
val myScope: CoroutineScope = CoroutineScope(myContext)
// returns immediately (unless you specify a start mode that run immediately)
val job = myScope.launch {
  // suspend calls are allowed here cause this is a coroutine
}
// this code is executed right away
Run Code Online (Sandbox Code Playgroud)

您可以从协程外部执行此操作(纯代码)。

coroutineScope()另一方面是一个全局挂起函数CoroutineScope,它在底层创建一个新的挂起函数,然后执行您在主体中传递的挂起函数,并等待它(及其所有子函数)完成后再返回。它是一个挂起函数,因此您不能在协程之外调用它。

// must be inside a coroutine here!

// this create a new CoroutineScope,
// then launch the given coroutine,
// then wait for it to complete
val result = coroutineScope {
   // your coroutine here, which run immediately
   return@coroutineScope "my result"
}
// this code is executed after the coroutine above is completed
// I can use "result" here
Run Code Online (Sandbox Code Playgroud)

coroutineScope与那里类似,supervisedScope只有 1 个区别:在其中执行的多个子协程(启动/异步/...)如果一个子协程失败导致它使用一个子协程,则不会取消其他子协程SupervisorJob