本文介绍了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 –

当协程既不占用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.

您可以详细了解此处此处

这篇关于Kotlin协程选择Dispatcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 07:58