Rxjava: Should/Why doesn't Single.zip() have a delayError override?

Created on 1 Nov 2018  路  7Comments  路  Source: ReactiveX/RxJava

I noticed that Single.zip() doesn't have an overload that takes in delayError. The only references I can find are here https://github.com/ReactiveX/RxJava/issues/5339#issuecomment-301854706 (simply stating that it doesn't exist, no explanation why), and this https://github.com/ReactiveX/RxJava/issues/4871#issuecomment-262278563 (which implies that there should be one)

So should this method exist? If it's already been discussed, what were the reasons to not include it?

2.x Question

All 7 comments

Zip is supposed to wait for a row of items to combine, but a failing Single will never produce an item thus a full row. DelayError serves as a means to get as much full rows as possible before failing instead of stopping at the first sign of error. This doesn't translate to single.

Ok, maybe I misunderstand the delayError flag. I thought it would combine any errors into a CompositeException, which you hint at here:

There should be an overload which lets you delay all errors so you really get one CompositeException if there were more than one.

https://github.com/ReactiveX/RxJava/issues/4871#issuecomment-262278563

I see how this flag works on other operators and now I see what you mean. delayError just keeps the error from "jumping to the front of the queue, allowing any onNext's to be emitted first"... which now makes sense why there isn't an operator for Single.zip().

So to my underlying problem. Is there a way I can force Single.zip() to combine all errors into a CompositeException?

Is there a way I can force Single.zip() to combine all errors into a CompositeException?

No as it would have to wait for all sources to terminate, which is not supported.

The workaround is to not have errors but use Notification in each source and now you get an array of mixed errors and values you can combine yourself into a composite.

Of course, there is always the option for you to implement a zip that behaves as you want since it's a static method and can live on a class you write; using e.g., MyZip.zip instead of Single.zip.

Nice! I had started to roll my own value/error helper class. I didn't know about Notification! Just curious why does Single not have a materialize() operator?

For those who see this later, here's how I solved this:

Single<Object> single1 = ...
Single<Object> single2 = ...

Single<Notification<Object>> notification1Single = single1
        .toObservable()
        .materialize()
        .firstOrError();

Single<Notification<Object>> notification2Single = single2
        .toObservable()
        .materialize()
        .firstOrError();

Single.zip(notification1Single, notification2Single,
        (notification1, notification2) -> {
            if (notification1.isOnError() && notification2.isOnError()) {
                throw new CompositeException(notification1.getError(), notification2.getError());
            } else if (notification1.isOnError()) {
                throw Exceptions.propagate(notification1.getError());
            } else if (notification2.isOnError()) {
                throw Exceptions.propagate(notification2.getError());
            }

            // TODO else combine 1 and 2 into new object as normal
            return ....
        });

Just curious why does Single not have a materialize() operator?

Because you didn't look at the current Issue list/PRs.

;) touche

You can resolve it like this:

Observable.zip( responseOneObservable .onErrorReturn(new Function<Throwable, ResponseOne>() { @Override public ResponseOne `apply(@NonNull` final Throwable throwable) { return new ResponseOne(); } }), responseTwoObservable .onErrorReturn(new Function<Throwable, ResponseTwo>() { @Override public ResponseTwo apply(@NonNull final Throwable throwable) { return new ResponseTwo(); } }), ...

https://stackoverflow.com/questions/41342844/rxjava-how-to-handle-error-with-zip-operator

Was this page helpful?
0 / 5 - 0 ratings

Related issues

francorolando picture francorolando  路  3Comments

archenroot picture archenroot  路  3Comments

dlew picture dlew  路  4Comments

perlow picture perlow  路  3Comments

dsvoronin picture dsvoronin  路  4Comments