簡體   English   中英

將Dispatcher.Main與delay()一起使用是個好主意嗎?

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

fetchData()suspendCoroutine函數,因此在其他線程上實現。

viewModelScope綁定到Dispatchers.Main :僅應用於與UI交互並執行快速工作。

那么我應該在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 ->}
}

答案是它可以留下, delay不會傷害主線程,也不會阻塞它。 Main Dispatcher內部的協程將被暫停,而Main內部的其他協程將繼續運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM