Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 554
sockeqwe Avatar asked Dec 07 '16 12:12

sockeqwe


1 Answers

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.

like image 184
tynn Avatar answered Sep 28 '22 04:09

tynn