Kotlin协程选择Dispatcher [英] Kotlin Coroutines choosing Dispatcher

查看:557
本文介绍了Kotlin协程选择Dispatcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出在哪种情况下使用哪个Dispatcher.

I'm trying to figure out which Dispatcher to use in what case.

我阅读了一些文档,并提出了以下建议:

I read some documentation and came up with this:

  • 默认→CPU工作量
  • 主要→主要(更新用户界面)
  • 不受限制
  • IO→编写文件/网络任务

这是对的吗

无限制的怎么办?

推荐答案

您对调度程序类型的假设是正确的.无限制的调度员–

You're right in your assumptions about the dispatcher types. Unconfined dispatcher –

不限于任何特定线程的协程分派器

A coroutine dispatcher that is not confined to any specific thread

当协程既不占用CPU时间也不更新限于特定线程的任何共享数据(如UI)时,无限制调度程序是合适的.

Unconfined dispatcher is appropriate when coroutine does not consume CPU time nor updates any shared data (like UI) that is confined to a specific thread.

您可以详细了解 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆