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.
Even this doesn't work:
doSomeBackgroundWork()
.concatMap(s -> call() {
mApi.getData()
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<..>() { ... })
Also, when I try this the problem is that zipWith
means the two observables run in parallel and I really want then to run one after the other.
doSomeBackgroundWork()
.zipWith(mApi.getData()),
new Func2<BgWork, DataResponse,DataResponse>() {
@Override
public DataResponse call(BgWork bgWork, DatResponse data) {
return data;
}})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<..>() { ... })