Rxjava: Feature Request: observable methods to supress onComplete, onError and onNext.

Created on 4 Aug 2014  路  7Comments  路  Source: ReactiveX/RxJava

Observable observable;

observable.suppressOnComplete()
observable.suppressOnError()
observable.suppressOnNext()

The resulting observable would be guaranteed to never emit the respective notification.

This can be implemented as a custom operator with lift, but if it is commonly needed maybe it should be added to the API.

In particular, these are useful for the event bus use cases. ( e.g. observable.suppressOnComplete().subscribe(myPublishSubject). )

Question

Most helpful comment

What is the use case that results in terminal events being propagated that you want to ignore?

Interested in the why as well, also because typically when you subscribe, you just pass in onNext. Moreover, you can always concatenate Observable.never at the end to ignore onCompleted and onError. However, I don't think it is smart, or useful, to do that.

All 7 comments

Are you trying to subscribe a single PublishSubject to multiple finite Observables?

What is the use case that results in terminal events being propagated that you want to ignore?

Here is a simple example that will never terminate: https://gist.github.com/benjchristensen/04eef9ca0851f3a5d7bf

What is the use case that results in terminal events being propagated that you want to ignore?

Interested in the why as well, also because typically when you subscribe, you just pass in onNext. Moreover, you can always concatenate Observable.never at the end to ignore onCompleted and onError. However, I don't think it is smart, or useful, to do that.

@benjchristensen yes, one Subject subscribing to multiple finite observables. For example, observables returned by Retrofit services ( http://square.github.io/retrofit/ ). The observables returned by Retrofit emit one item and then complete or just error. But you might want to "pipe" the results into one Subject.

Aha, I now see what you do .subscribe(myPublishSubject). which is not the way to merge streams, and yes in that case the subject will be closed. Instead you simply https://github.com/Netflix/RxJava/wiki/Combining-Observables the streams.

In general you should try to avoid subjects, and definitively avoid using them inside subscribe.

@yogurtearl As per @headinthebox you should use merge, zip or other combinatorial operators to achieve what you're doing. It is almost always wrong to need the use of a Subject.

Here is example code using flatMap and zip to combine multiple network calls: https://github.com/benjchristensen/ReactiveLab/blob/master/reactive-lab-edge/src/main/java/io/reactivex/lab/edge/routes/RouteForDeviceHome.java#L51

A lot of our use cases are event bus (-ish)... Is there a recommended way to implement event buses without Subjects?

You need subject when (a) you want to share side-effects of a computation; in many case you can do that locally by using xs.publish(xs -> {}), or (b) you want to turn a cold observable into a hot observable (which technically is sharing side effects as well).

In many case when you think you need a subject as a source/producer of events, you can do without one, for example use Observable.create instead.

The other thing to note is that once you do a subscribe you should assume you are leaving the Rx world, i.e. you update the UI or perform some side-effects to write data to a file or something. As a rule of thumb you never use subjects inside a subscribe because that is an indication that there should be an operator you use to transform the stream _before_ you subscribe.

Was this page helpful?
0 / 5 - 0 ratings