[英]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.