I concat two observables to display data from cache firstly and after that start loading data from the network and show updated data.
Observable.concat(
getContentFromCache.subscribeOn(dbScheduler),
getContentFromNetwork.subscibeOn(networkScheduler)
).observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
If there is no network connection the second observable fails immediately after OnSubscribe is called.
In case the second observable fails immediately, data from first observable is lost. The onNext method is never called in the subscriber.
I think, this might be due to the following code in the OperatorConcat.ConcatSubscriber
@Override
public void onNext(Observable<? extends T> t) {
queue.add(nl.next(t));
if (WIP_UPDATER.getAndIncrement(this) == 0) {
subscribeNext();
}
}
@Override
public void onError(Throwable e) {
child.onError(e);
unsubscribe();
}
Looks like after error is received it unsubscribes, and all pending onNext are lost.
What is the best way to solve my problem?
Update
Looks like I have found the solution, instead of setting observOn for concatenated observable I set observOn for each observable.
Observable.concat(
getContentFromCache.subscribeOn(dbScheduler).observeOn(AndroidSchedulers.mainThread()),
getContentFromNetwork.subscibeOn(networkScheduler).observeOn(AndroidSchedulers.mainThread())
)
.subscribe(subscriber);