diff --git a/core-base/ui/src/commonMain/kotlin/template/core/base/ui/BaseViewModel.kt b/core-base/ui/src/commonMain/kotlin/template/core/base/ui/BaseViewModel.kt index 2b4b8550..c40b0be5 100644 --- a/core-base/ui/src/commonMain/kotlin/template/core/base/ui/BaseViewModel.kt +++ b/core-base/ui/src/commonMain/kotlin/template/core/base/ui/BaseViewModel.kt @@ -11,6 +11,9 @@ package template.core.base.ui import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.SendChannel import kotlinx.coroutines.flow.Flow @@ -97,4 +100,18 @@ abstract class BaseViewModel( protected fun sendEvent(event: E) { viewModelScope.launch { eventChannel.send(event) } } + + /** + * Launches a coroutine on [Dispatchers.Default] 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. + * + * Note: Uses Default dispatcher for multiplatform compatibility (IO is not available on JS). + * + * @param block The suspending block to execute on the background dispatcher. + * @return The [Job] representing the coroutine. + */ + protected fun launchIO(block: suspend CoroutineScope.() -> Unit): Job { + return viewModelScope.launch(Dispatchers.Default, block = block) + } }