fetchData
Executes a suspending source on Dispatchers.IO and delivers the Result to onResult on Dispatchers.Main.
Errors are caught and forwarded as Result.failure — the ViewModel scope is never cancelled by a failing source.
fetchData(
source = { repository.getUser(userId) },
onResult = { result ->
result
.onSuccess { user ->
updateState { copy(user = user, isLoading = false) }
}
.onFailure { error ->
updateState { copy(isLoading = false) }
sendEvent(ProfileEvent.ShowToast("Failed to load user"))
}
}
)Content copied to clipboard
Parameters
source
Suspending function executed on the IO dispatcher.
onResult
Callback receiving a Result wrapping the value or error, always invoked on the Main dispatcher.
Type Parameters
T
The type of data returned by source.