简体   繁体   English

将Dispatcher.Main与delay()一起使用是个好主意吗?

[英]Is it good idea to use Dispatcher.Main with delay( )?

fetchData() is suspendCoroutine function, so it is implemented on other thread. fetchData()suspendCoroutine函数,因此在其他线程上实现。

viewModelScope is bound to Dispatchers.Main : this should be used only for interacting with the UI and performing quick work. viewModelScope绑定到Dispatchers.Main :仅应用于与UI交互并执行快速工作。

So should I have delay() in Dispatcher.Main or should I move out it? 那么我应该在Dispatcher.Main使用delay()还是将其移出?

fun loadData() {
    viewModelScope.launch {
      delay(START_DELAY)
      when (val result = fetchData()) {
        is Response.Success<IData> -> {}
        is Response.Failure -> {}
      }
    }
  }


fun fetchData(){
    return suspendCoroutine { cont ->}
}

Answer is it can stay, delay will not hurt Main thread, it will not block it. 答案是它可以留下, delay不会伤害主线程,也不会阻塞它。 This coroutine inside Main Dispatcher will be suspended, while other coroutines inside Main will continue to run. Main Dispatcher内部的协程将被暂停,而Main内部的其他协程将继续运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM