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