Skip to content

Instantly share code, notes, and snippets.

@ian-ellis
Created January 17, 2017 22:32
Show Gist options
  • Save ian-ellis/570df2ad6a1d4eaf7018b6f9b4af8ea8 to your computer and use it in GitHub Desktop.
Save ian-ellis/570df2ad6a1d4eaf7018b6f9b4af8ea8 to your computer and use it in GitHub Desktop.
Example of how to swallow an error without ending the parent stream
public static <T, R> Observable.Transformer<T, R> switchMapSwallowError(Func1<T, Observable<R>> onwardCall) {
return new Observable.Transformer<T, R>() {
private R previousValue = null;
@Override
public Observable<R> call(Observable<T> observable) {
return observable.switchMap(
a -> onwardCall.call(a)
.doOnNext(value -> previousValue = value)
.onErrorReturn(e -> previousValue)
);
}
};
}
BehaviorSubject<Void> tigger = BehaviorSubject.create();
trigger.compose(switchMapSwallowError(aVoid->makeNetworkCall())).subscribe(next->{
},e->{})
tigger.onNext(null)//causes call to makeNetworkCall
tigger.onNext(null)//causes call to makeNetworkCall which if it throws an error will return previous value
tigger.onNext(null)//causes call to makeNetworkCall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment