sockeqwe
sockeqwe

Reputation: 15929

How to get a Disposable for subjects as subscriber in RxJava2

It might be a silly question, but how do I get a Disposable when subscribing a Subject to an Observable in RxJava 2.0?

For example:

observable.subscribeWith( behaviorSubject)

doesn't return a Disposable? How do I cancel such a subscription?

Or another example with CompositeDisposable:

compositeDisposable.add( observable.subscribeWith( behaviorSubject) ) ) 

This doesn't compile because subscribeWith( behaviorSubject ) doesn't return a Disposable.

How do I unsubscribe / dispose / cancel properly with Subjects?

Upvotes: 8

Views: 2318

Answers (1)

tynn
tynn

Reputation: 39853

You're not the first one to stumble about this. For example issue #4438 is about it. Just wrap your subject with DisposableObserver. In another issue about Flowable someone proposed to use one of the take*() methods to complete the subscription.

Upvotes: 3

Related Questions