Flatmap Single into Observable and ignore errors
See original GitHub issueI have UI observable which should work as long as my UI lives. When my UI emits I create a network request which could be an Observable or a Single. A Single fits best for a network call.
RxView.clicks(myButton)
.flatMap(v - > networkRequest())
.subscribe(data -> showSomething(data),
e -> {
// should only be called when the RxView.clicks() throws.
// should not be called for network errors
});
When using an Observable I would use onErrorResumeNext
returning Observable.empty()
to prevent errors coming from the network request going into my UI Observable and calling onError
because the UI Observable should life forever:
Observable networkRequest() {
mApiService.fireActionObservable()
.onErrorResumeNext(throwable -> {
// somehow handle error here
return Observable.empty();
});
}
This, in my opinion, elegant way does not work for Single because no Single.empty()
exists.
Single networkRequest() {
mApiService.fireActionSingle()
.onErrorResumeNext(throwable -> {
// somehow handle error here
return Single.empty(); // <-- does not exist. I have to call success or error :/
});
}
Single.onErrorResumeNext
is btw only available in 2.x
Converting my Single
to an Observable
seems wrong, because the network request is a Single
!
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
How to ignore error and continue infinite stream?
When I add onExceptionResumeNext(Observable.empty()) after the flatMap(locations -> mRestService.postLocations(locations)) onCompleted is invoked and stream ...
Read more >How to ignore error and continue infinite stream? : r/androiddev
I saw your message, and I checked the link. You wrote "When I add onExceptionResumeNext(Observable.empty()) after the flatMap(locations -> mRestService.
Read more >FlatMap operator - ReactiveX
FlatMap. transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable. FlatMap.
Read more >RxJava and Error Handling - Baeldung
In this article, we'll take a look at how to handle exceptions and errors using RxJava. First, keep in mind that the Observable...
Read more >RxJava — Handling Errors Like a Pro | by TC Wang - Medium
Place retryWhen() right after the Observable/Single/Maybe , i.e. a cold stream, whenever you want to perform a back-off. This retryWhen provides ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Single.empty()
doesn’t make sense becauseSingle
has to emit either anonSuccess
or anonError
. The best you can do is to have aSingle.just()
with a normal value that represents emptiness.Generally, this is why I’m skeptic about
Single
andCompletable
becauseObservable
can “emulate” both and the overhead in true async use is negligible.My solution: RxView.clicks(myButton) .flatMap(v - > networkRequest()) .compose(exceptionHandling()) .subscribe(data -> showSomething(data), e -> { // Now you should remove this lambda as it will never be called. }); Final: RxView.clicks(myButton) .flatMap(v - > networkRequest()) .compose(exceptionHandling()) .subscribe(data -> showSomething(data)); exceptionHandling: protected <T> ObservableTransformer<T, T> exceptionHandling() { return observable -> observable .doOnError(t ->{ /* handle error here */}) .retry(); // Ignore error to keep subscription. }