gpt4 book ai didi

android - GlobalScope 与 CoroutineScope 与生命周期范围

转载 作者:行者123 更新时间:2023-12-03 13:25:13 26 4
gpt4 key购买 nike

我习惯使用 AsyncTask并且由于它的简单性而很好地理解它。但是Coroutines让我很困惑。您能否以简单的方式向我解释以下各项的区别和目的是什么?

  • GlobalScope.launch(Dispatchers.IO) {}
  • GlobalScope.launch{}
  • CoroutineScope(Dispatchers.IO).launch{}
  • lifecycleScope.launch(Dispatchers.IO){}
  • lifecycleScope.launch{}
  • 最佳答案

    首先,让我们从定义开始说清楚。如果您需要 Coroutines 和 Coroutines Flow 的教程或游乐场,您可以查看 tutorial/playground我建立。Scope是您用来启动协程的对象,它只包含一个对象 CoroutineContext

    public interface CoroutineScope {
    /**
    * The context of this scope.
    * Context is encapsulated by the scope and used for implementation of coroutine builders that are extensions on the scope.
    * Accessing this property in general code is not recommended for any purposes except accessing the [Job] instance for advanced usages.
    *
    * By convention, should contain an instance of a [job][Job] to enforce structured concurrency.
    */
    public val coroutineContext: CoroutineContext
    }
    协程上下文是一组规则和配置,它们定义
    协程将如何执行。
    在底层,它是一种映射,具有一组可能的键和值。
    协程上下文是不可变的,但您可以使用加号运算符将元素添加到上下文中,
    就像您将元素添加到集合中一样,生成一个新的上下文实例
    定义协程行为的元素集是:
  • CoroutineDispatcher — 将工作分派(dispatch)到适当的线程。
  • Job——控制协程的生命周期。
  • CoroutineName — 协程的名称,用于调试。
  • CoroutineExceptionHandler — 处理未捕获的异常

  • 调度员
    调度程序确定应该使用哪个线程池。调度员类也是
    协程上下文 可以添加到 CoroutineContext
  • 调度员。默认 :CPU 密集型工作,例如对大型列表进行排序、进行复杂计算等。 JVM 上的共享线程池支持它。
  • 调度员.IO : 联网或读写文件。
    简而言之 - 任何输入和输出,如名称所述
  • Dispatchers.Main : 在 Android 的主线程或 UI 线程中执行 UI 相关事件的强制调度程序。

  • 例如,在 RecyclerView 中显示列表、更新 Views 等。
    您可以查看 Android's official documents有关调度程序的更多信息。
    编辑即使官方文件指出

    Dispatchers.IO - This dispatcher is optimized to perform disk ornetwork I/O outside of the main thread. Examples include using theRoom component, reading from or writing to files, and running anynetwork operations.


    来自 的回答马尔科·托波尔尼克

    IO runs the coroutine on a special, flexible thread pool. It existsonly as a workaround when you are forced to use a legacy, blocking IOAPI that would block its calling thread.


    也可能是对的。
    职位 协程本身由 Job 表示。
    Job 是协程的句柄。对于您创建的每个协程(通过启动或异步),
    它返回一个唯一标识协程并管理其生命周期的 Job 实例。
    您还可以将 Job 传递给 CoroutineScope 以掌握其生命周期。
    它负责协程的生命周期、取消和父子关系。
    可以从当前协程的上下文中检索当前作业:
    Job 可以经历一组状态:New、Active、Completing、Completed、Canceling 和Cancelled。
    虽然我们无法访问各州本身,
    我们可以访问 Job 的属性:isActive、isCancelled 和 isCompleted。
    协程范围 它定义了一个简单的工厂函数,它采用 CoroutineContext s 作为参数来围绕组合的 CoroutineContext 创建包装器为
    public fun CoroutineScope(context: CoroutineContext): CoroutineScope =
    ContextScope(if (context[Job] != null) context else context + Job())

    internal class ContextScope(context: CoroutineContext) : CoroutineScope {
    override val coroutineContext: CoroutineContext = context
    // CoroutineScope is used intentionally for user-friendly representation
    override fun toString(): String = "CoroutineScope(coroutineContext=$coroutineContext)"
    }
    并创建一个 Job如果提供上下文还没有一个元素。
    我们来看一下 GlobalScope 源码
    /**
    * A global [CoroutineScope] not bound to any job.
    *
    * Global scope is used to launch top-level coroutines which are operating on the whole application lifetime
    * and are not cancelled prematurely.
    * Another use of the global scope is operators running in [Dispatchers.Unconfined], which don't have any job associated with them.
    *
    * Application code usually should use an application-defined [CoroutineScope]. Using
    * [async][CoroutineScope.async] or [launch][CoroutineScope.launch]
    * on the instance of [GlobalScope] is highly discouraged.
    *
    * Usage of this interface may look like this:
    *
    * ```
    * fun ReceiveChannel<Int>.sqrt(): ReceiveChannel<Double> = GlobalScope.produce(Dispatchers.Unconfined) {
    * for (number in this) {
    * send(Math.sqrt(number))
    * }
    * }
    * ```
    */
    public object GlobalScope : CoroutineScope {
    /**
    * Returns [EmptyCoroutineContext].
    */
    override val coroutineContext: CoroutineContext
    get() = EmptyCoroutineContext
    }
    如您所见,它扩展了 CoroutineScope1- GlobalScope.launch(Dispatchers.IO) {}只要您的应用程序还活着,GlobalScope 就活着,如果您在此范围内进行一些计数并旋转您的设备,它将继续任务/过程。
    GlobalScope.launch(Dispatchers.IO) {} 
    只要您的应用程序还活着,但由于使用 Dispatchers.IO 而在 IO 线程中运行,就可以运行
    2- GlobalScope.launch{}它与第一个相同,但默认情况下,如果您没有任何上下文,则启动使用 EmptyCoroutineContext,它使用 Dispatchers.Default,因此唯一的区别是线程与第一个。
    3- CoroutineScope(Dispatchers.IO).launch{}这个和第一个一样,只是语法不同。
    4- lifecycleScope.launch(Dispatchers.IO){} lifecycleScopeLifeCycleOwner 的扩展并绑定(bind)到 Activity 或 Fragment 的生命周期,当该 Activity 或 Fragment 被销毁时,范围被取消。
    /**
    * [CoroutineScope] tied to this [LifecycleOwner]'s [Lifecycle].
    *
    * This scope will be cancelled when the [Lifecycle] is destroyed.
    *
    * This scope is bound to
    * [Dispatchers.Main.immediate][kotlinx.coroutines.MainCoroutineDispatcher.immediate].
    */
    val LifecycleOwner.lifecycleScope: LifecycleCoroutineScope
    get() = lifecycle.coroutineScope
    您也可以将其用作
    class Activity3CoroutineLifecycle : AppCompatActivity(), CoroutineScope {

    private lateinit var job: Job

    override val coroutineContext: CoroutineContext
    get() = job + Dispatchers.Main + CoroutineName("🙄 Activity Scope") + CoroutineExceptionHandler { coroutineContext, throwable ->
    println("🤬 Exception $throwable in context:$coroutineContext")
    }


    private val dataBinding by lazy {
    Activity3CoroutineLifecycleBinding.inflate(layoutInflater)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(dataBinding.root)

    job = Job()

    dataBinding. button.setOnClickListener {

    // This scope lives as long as Application is alive
    GlobalScope.launch {
    for (i in 0..300) {
    println("🤪 Global Progress: $i in thread: ${Thread.currentThread().name}, scope: $this")
    delay(300)
    }
    }

    // This scope is canceled whenever this Activity's onDestroy method is called
    launch {
    for (i in 0..300) {
    println("😍 Activity Scope Progress: $i in thread: ${Thread.currentThread().name}, scope: $this")
    withContext(Dispatchers.Main) {
    dataBinding.tvResult.text = "😍 Activity Scope Progress: $i in thread: ${Thread.currentThread().name}, scope: $this"
    }
    delay(300)
    }
    }
    }

    }

    override fun onDestroy() {
    super.onDestroy()
    job.cancel()
    }

    }

    关于android - GlobalScope 与 CoroutineScope 与生命周期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65008486/

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