Reputation: 297
fetchData()
is suspendCoroutine
function, so it is implemented on other thread.
viewModelScope
is bound to Dispatchers.Main
: this should be used only for interacting with the UI and performing quick work.
So should I have delay()
in Dispatcher.Main
or should I move out it?
fun loadData() {
viewModelScope.launch {
delay(START_DELAY)
when (val result = fetchData()) {
is Response.Success<IData> -> {}
is Response.Failure -> {}
}
}
}
fun fetchData(){
return suspendCoroutine { cont ->}
}
Upvotes: 3
Views: 1448
Reputation: 10038
Answer is it can stay, delay
will not hurt Main thread, it will not block it. This coroutine inside Main Dispatcher will be suspended, while other coroutines inside Main will continue to run.
Upvotes: 10