Shahzad Aslam
11/19/2019, 11:17 PM////// Approach 2 /////////
class ViewModel2(
private val interactor: Interactor,
private val dispatchers: ICoroutineContextProvider
) {
private val scope = CoroutineScope(SupervisorJob() + dispatchers.main)
fun getSomething() {
scope.launch {
val resultOrError = withContext(<http://dispatchers.io|dispatchers.io>) {
interactor.fetchSomething()
}
resultOrError.either(::handleError, ::handleData)
}
}
private fun handleData(i: Int) { /* set/update LiveData */ }
private fun handleError(error: Error) { /* set/update LiveData */ }
}
class Interactor2 {
suspend fun fetchSomething(): Either<Error, Int> = coroutineScope {
// some very heavy operation
Either.Result(5)
}
}