Created
January 17, 2017 22:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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