【问题标题】:Difference between CoroutineScope and coroutineScope in KotlinKotlin 中 CoroutineScope 和 coroutineScope 的区别
【发布时间】:2020-04-09 15:18:02
【问题描述】:

谁能在函数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)
    }

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

【问题讨论】:

    标签: android kotlin kotlin-coroutines suspend coroutinescope


    【解决方案1】:

    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

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

    解决方案:

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

    这可以通过在子/被调用者协程内重用父/调用者协程范围来完成。这可以通过子/被调用协程内的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

    【讨论】:

    • 为什么它看起来像java代码?例如。 int result = 0 而不是 var result: Int = 0extends 关键字而不是 : 另一方面,您可以看到来自 Kotlin 语言的 suspend 关键字。这是怎么回事?
    • @Kushal,我将这个半 koitlin 代码修改为 kotlin 代码。如果您保留原帖,请恢复原帖。我测试了这段代码,我认为它似乎适合你的目的。
    • 完全删除coroutineScope会不会一样?
    • 最好的解释!!点赞。
    【解决方案2】:

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

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

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

    【讨论】:

    • 那么,coroutineScope()withContext() 被认为是协程构建器?
    • 不错的答案,但 coroutineScope 基本上是 withContext(this.coroutineContext) 的别名并不是真的:它们不是一回事。 withContext 不会等待你在其中午餐的子协程完成,coroutineScope
    • @DanieleSegato 你试过了吗,还是你说它只是基于文档?因为文档已经过时,withContext 现在does wait 用于完成子协程。
    • 哦,你是对的!不,我没有尝试,我只是查看了代码,看起来他们做了不同的事情(我更详细地回答你here
    • @AbhijitSarkar 如果您使用CoroutineScope().launch,您将犯该博客中提到的完全相同的错误。它们几乎是同义词。
    【解决方案3】:

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

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

    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
    

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

    另一方面,

    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
    

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

    【讨论】:

      【解决方案4】:

      CoroutineScope() 是将Context 作为输入并为您提供ContextJob 作为CoroutineScope 接口对象的方法。

      您可以使用此对象启动协程作业,如下所示:

      suspend fun doNotDoThis() {
        CoroutineScope(coroutineContext).launch {
            println("I'm confused")
        }
      }
      

      coroutineScope() 需要block/labmda 作为协程job 执行:

         fun main() = runBlocking { // this: CoroutineScope
          launch { 
              delay(200L)
              println("Task from runBlocking")
          }
      
          coroutineScope { // Creates a new coroutine scope
              launch {
                  delay(500L) 
                  println("Task from nested launch")
              }
      
              delay(100L)
              println("Task from coroutine scope") // This line will be printed before nested launch
          }
      
          println("Coroutine scope is over") // This line is not printed until nested launch completes
      }
      

      参考文献:

      Article-1

      Article-2

      Kotlin-Docs

      我希望这能回答你的问题。

      【讨论】:

        【解决方案5】:

        在非结构化并发示例中,如果您将 Launch builder 替换为 Async 并在 deferred 上等待,它将与您在结构化并发中使用的示例相同。你的答案还不清楚。解释结构化并发的实际使用(这在异常和错误处理中很有用,当其中一个子作业抛出异常时,应该不会影响其他子作业(作业))

        【讨论】:

          猜你喜欢
          • 2021-01-02
          • 2019-11-13
          • 2022-11-12
          • 1970-01-01
          • 2020-08-23
          • 1970-01-01
          • 1970-01-01
          • 2020-11-21
          • 1970-01-01
          相关资源
          最近更新 更多