Artur A
Artur A

Reputation: 297

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

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

Answers (1)

Jemshit
Jemshit

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

Related Questions