Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ package template.core.base.ui

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -97,4 +101,16 @@ abstract class BaseViewModel<S, E, A>(
protected fun sendEvent(event: E) {
viewModelScope.launch { eventChannel.send(event) }
}

/**
* Launches a coroutine on [Dispatchers.IO] for network or database operations.
* Use this instead of `viewModelScope.launch` for any I/O-bound work to avoid
* blocking the main thread and causing UI freezes.
*
* @param block The suspending block to execute on the IO dispatcher.
* @return The [Job] representing the coroutine.
*/
protected fun launchIO(block: suspend CoroutineScope.() -> Unit): Job {
return viewModelScope.launch(Dispatchers.IO, block = block)
}
}
Loading