Mortada
Mortada

Reputation: 199

Rxjava 2 Maybe.toSingle error

why maybe.toSingle() throw error no such element? I tried to handle doOnError but doesn't work!!

     Single<Integer> singleOdd = Single.just(1);
    Single<Integer> singleEven = Single.just(2);
      Single.concat(singleOdd.filter(integer -> integer%2 ==0).toSingle(),singleEven).doOnError(throwable -> throwable.printStackTrace()).subscribe();

Upvotes: 0

Views: 2068

Answers (1)

akarnokd
akarnokd

Reputation: 69997

why maybe.toSingle() throw error no such element?

filter() on a Single has two outcomes, either it passes and you have one item, or it doesn't pass and you have an empty Maybe. Converting back to Single mandates that you have exactly one item or an error.

I tried to handle doOnError but doesn't work!!

doOnError is not error handling from the stream's perspective but a peek into the error channel. You have to use onErrorResumeNext or retry to react to an error case.

Upvotes: 5

Related Questions