question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flatmap Single into Observable and ignore errors

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
akarnokdcommented, Oct 27, 2015

Single.empty() doesn’t make sense because Single has to emit either an onSuccess or an onError. The best you can do is to have a Single.just() with a normal value that represents emptiness.

Generally, this is why I’m skeptic about Single and Completable because Observable can “emulate” both and the overhead in true async use is negligible.

0reactions
ShabanKamellcommented, Oct 14, 2017

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. }

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found