Search code examples
rx-java2

RxJava: Merge multiple singles and complete after some have failed


I would like to merge two Single<MyData> such that if one of them fails but the other one succeeds then the error of the one that failed and the emission from the other one are reported, and then the resulting Single<MyData> (or Observable<MyData>) completes.

If both Single<MyData> fail then the result should also fail and also be marked as failed.

What I would like to have at the end is:

  • If both succeed then the emitted values and a producer marked as completed.
  • If one succeeds and the other fails, the emitted value, the thrown error and the producer marked as complete.
  • If all fail, the errors and the producer marked as failed.

It's like an 'OR' operation


Solution

  • This is not possible. There is only a single terminal event allowed. The contract for Single is success|error. If you need to receive a next event as well, you should consider to use Observable instead. The contract for Observable is next* complete|error, but you'll still not get a complete.

    Observable.mergeDelayError(single1.toObservable(), single2.toObservable())