Scott Kruse
07/27/2021, 9:36 PMviewModelScope.launch {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
doStuff()
}
}
suspend fun doStuff() {
doStuffWithRx()
}
fun doStuffWithRx() {
Single.fromCallable {
// hit network
}
.subscribeOn(<http://Schedulers.io|Schedulers.io>())
.observeOn(AndroidSchedulers.mainThread())
.doOnSuccess { result ->
}
.subscribe()
}
Casey Brooks
07/27/2021, 9:48 PMSingle
is not bound to the coroutine in any way. It’s just running in the background, completely unmanaged. There’s a coroutines artifact specifically for connecting RxJava to Coroutines (and vice-versa) which you should be using here, Single.await()
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx2/index.htmlNick Allen
07/27/2021, 9:48 PMdoStuff
will not throw an exception.