Rxjava: Completable missing lambda-subscibe() w/ onSubscribe parameter to provide Disposable

Created on 21 Sep 2017  路  13Comments  路  Source: ReactiveX/RxJava

Completable does not have a lambda subscribe() method that provides a Disposable via an onSubscribe parameter, equivalent to the one implemented in Observable:

public final Disposable subscribe(
                    Consumer<? super T> onNext,
                    Consumer<? super Throwable> onError,
                    Action onComplete,
                    Consumer<? super Disposable> onSubscribe)
2.x Feature-Request

Most helpful comment

I guess we can start creating issues marked with Design and 3.x so we don't forget these.

All 13 comments

Neither has Single or Maybe. I'm not convinced we really need it. Do you have an use case where having the Disposable returned by subscribe() is not enough?

Never had the need for it in Flowable / Observable either.

Someone can be confused because Wiki describes that Completable provides a Disposable with an onSubscribe,,

So, I think it need for them to modify wiki or to provide onSubscribe..

https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#completable

That is on the default consumer type CompletableObserver and is part of the protocol. You can implement CompletableObserver and gain access to the Disposable. We just don't provide that angle via lambdas.

@akarnokd The Disposable returned by subscribe() certainly works. It may be a personal preference, but I prefer the neatness of adding to my CompositeDisposable in a subscribe() lambda vs wrapping the entire Rx chain in a CompositeDisposable.add(). And given that Observable has such an overload of subscribe(), I would think that for consistency a case could be made for adding an equivalent overload to the other Rx types.

I'm not convinced we need such overloads. @artem-zinnatullin your opinion?

Yeah, I'm not a user of this API either.

@HolySamosa maybe you can use Kotlin? Then you can declare operator + for CompositeDisposable (RxKotlin does this) and use it like this:

compositeDisposable += Observable.fromCallable { something() }.subscribe()

If not, you could try extracting resulting Disposable into a local variable instead of wrapping whole chain with subscribe() call into a CompositeDisposable.add(), that's what I used to do using Java back in RxJava 1.x days.

If none of these options work for you, we of course can re-evaluate addition of this API and I understand the point about consistency between other rx types, but so far it doesn't look like something actively requested by users of the library.

I think the even the Observable/Flowable overloads are unnecessary after all. You can extend the DisposableXObserver and have onStart() save this into a composite.

Also there is the subscribeWith operator and you can create your own helper method that delegates to a DisposableXObserver:

source.subscribeWith(RxUtils.withLambdas(v -> { }, e -> { }, () -> { }, s -> { }));

Verdict: no.

Let's remove them in 3.x?

Sure.

Should we track this here in an issue or remove it in your preview?

Another thing that I remember is that there was a naming inconsistency regarding blockingGet in Completable in which which in the end we decided that the current name is not the best and we might rename it in 3.x

I guess we can start creating issues marked with Design and 3.x so we don't forget these.

@artem-zinnatullin Whoops, I guess I never hit the "Comment" button on my earlier reply.

Thanks for the suggestions, I can certainly work with them. And it looks like there is a plan to resolve the consistency issue in v3.

This is somewhat of a related tangent, but I wanted to get your thoughts before making a new issue. I think the documentation should make it clear that use of the .doOnSubscribe() should be avoided to acquire a Disposable for purposes of disposing / unsubscribing from the Rx chain. I had been doing this until I ran into an issue with dispose() not working as expected and after a fair amount of digging I discovered this SO answer by @akarnokd which schooled me in the error of my ways.

Was this page helpful?
0 / 5 - 0 ratings