I try to write a transformation function which is used with compose()
in order to reduce boilerplate code. It's pretty simple like this:
fun <R> withSchedulers(): ObservableTransformer<R, R> {
return ObservableTransformer {
it.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
}
So everytime I want to subscribe to anything on ioThread
and listen the result on mainThread
, it's just few lines of code:
Observable.just(1)
.compose(MyUtilClass.withSchedulers())
.subscribe()
But there isn't Observable
only, but we also have Single
, Completable
, Maybe
and Flowable
. So every time I want to combine them with my withSchedulers()
function, I have to transform it into the new type (which I don't expect).
For example,
Completable.fromAction {
Log.d("nhp", "hello world")
}//.compose(MyUtilClass.withSchedulers()) <-- This is not compiled
.toObservable() <--- I have to transform it into Observable
.compose(MyUtilClass.withSchedulers())
.subscribe()
So my question is, is there any way to write the above function to use with compose()
for any kind of Observable
(Single
, Completable
,...) ? Or we have to write different functions which use ObservableTransformer
, SingleTransformer
, ....?