Last active
March 18, 2020 14:48
-
-
Save FilipeLipan/5aec101638cb71782ddd251284d627ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
viewModelScope.launch { | |
runWithExecutor( | |
asyncFunction = async { getSupportedCountriesUseCase.execute(UseCase.None()) }, | |
contextExecutor = coroutinesDispatcherProvider.io) { | |
it.fold(::showError, ::showCountries) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> CoroutineScope.runWithExecutor( | |
asyncFunction: Deferred<Either<Failure, T>>, | |
contextExecutor: CoroutineContext, | |
onNext: (Either<Failure, T>) -> Unit = {}) { | |
this.launch { | |
val result: Either<Failure, T> = withContext(contextExecutor) { | |
asyncFunction.await() | |
} | |
withContext(Dispatchers.Main) { | |
onNext(result) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class CoroutinesDispatcherProvider { | |
open val main: CoroutineContext by lazy { Dispatchers.Main } | |
open val io: CoroutineContext by lazy { Dispatchers.IO } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment