We're refactoring our project from RX to Kotlin Coroutines, but not in one go, so we need our project use both for some time.
Now we have a lot of methods that use RX single as a return type like this, because they are heavy, long running operations, like API calls.
fun foo(): Single<String> // Heavy, long running opertation
We want it to be like this:
suspend fun foo(): String // The same heavy, long running opertation
Where we use this method, we want to still use RX.
We have been doing this:
foo()
.subscribeOn(Schedulers.io())
.map { ... }
.subscribe { ... }
Now how should I convert my suspend fun to produce a Single, that I can work with?
Is this a good idea?
Single.fromCallable {
runBlocking {
foo() // This is now a suspend fun
}
}
.subscribeOn(Schedulers.io())
.map { ... }
.subscribe { ... }