Kotlin中的CoroutineScope和coroutineScope之间的区别

时间:2019-12-17 06:42:01

标签: android kotlin kotlin-coroutines suspend 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())

/**
 * 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)
    }

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

5 个答案:

答案 0 :(得分:2)

CoroutineScope(大写C版本)与coroutineScope(小版本c)之间的最大区别是,我可以弄清楚,哪一个易于理解的原因是将它们与 Unstructured 结构化并发

让我分享一个例子:

class MainActivity extends Activity {
    private Button btn;
    public void onCreate(Bundle b) {
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.start_btn);
        btn.setOnClickListener( () -> {
        // Starting a coroutine in Main scope, to download user data, and will print it
        CoroutineScope(Dispatchers.Main).launch {
            int result = downloadUserData()
            Toast.makeText(applicationContext, "Result : " + result, Toast.LENGTH_LONG).show()
        });
    }

    private suspend int downloadUserData() {
        int 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 (int i = 0; i < 2000; i++) {
                kotlinx.coroutines.delay(400);
                result++;
            }
        }

        return result;
    }
}

输出: Result : 0

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

解决方案:

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

这可以通过在子/被调用协程中重用父/调用方协程范围来完成。这可以通过在子程序/被调用程序协程中的coroutineScope {}(Smaller c)版本来实现。

    private suspend int downloadUserData() {
        int 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 (int i = 0; i < 20000; i++) {
                kotlinx.coroutines.delay(400);
                result++;
            }
        }
        return result;
    }

输出:Result : 20000

答案 1 :(得分:2)

它们是完全不同的两件事。

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

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

另一方面,

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的{​​{1}}仅有1个区别:在内部执行的子协程(启动/异步/ ...)不会失败,因为它使用{{ 1}}

答案 2 :(得分:1)

CoroutineScope()是一种以Context作为输入并为Context提供Job作为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

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

答案 3 :(得分:1)

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

您很少(如果有的话)需要调用CoroutineScope(),因为通常您会选择一个现有的协程作用域或由其他便利功能(例如Android上的MainScope)为您创建一个协程作用域或Kotlin内部。

另一方面,

coroutineScope()是协程构建器之一,这意味着它执行您在子协程内部传递的块。它基本上是withContext(this.coroutineContext)的别名,当您要在前台继续进行一些工作时启动一个或多个背景协程时,应首先使用它,然后在完成代码块时加入背景协程。

答案 4 :(得分:0)

在“非结构化并发”示例中,如果将“启动”构建器替换为“异步”并等待延迟,则其工作原理与在“结构化并发”中使用的示例相同。您的答案仍不清楚。解释结构化并发的实际用法(在子Job中的一个抛出异常时,该异常在错误和错误处理中很有用)

相关问题