These are chat archives for ReactiveX/RxJava

10th
Jan 2017
Ashok Raju
@rajuashok
Jan 10 2017 03:30

Hi there, I'm trying to figure out how to chain to observables together. I have an existing method: public static Observable<Data> getData(). In my other class I have this existing code:

doSomeBackgroundWork()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<..>() { ... })

I'd like to now chain the getData() call to this call. How would I do this? I tried This initially:

doSomeBackgroundWork()
.flatMap(s -> call() {
   mApi.getData()
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<..>() { ... })

But this doesn't work, because the getData() code is actually executed on the main thread.

I need it to execute on the background thread just like the doSomeBackgroundWork() method.
Ashok Raju
@rajuashok
Jan 10 2017 05:14

This ended up working for me:

doSomeBackgroundWork()
.flatMap(s -> call() {
   return Observable.just(Data.empty());
}
.concatWith(mApi.getData().onErrorReturn(...))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<..>() { ... })

There has to be a cleaner way of doing this right?