These are chat archives for ReactiveX/RxJava

14th
Feb 2018
m0rgan
@m0rgan_twitter
Feb 14 2018 08:58 UTC
Hi guys!
(and girls)
I'm new here and in the Rx(Java/Swift) world, and I have a very dummy question :)
I want to create a PublishSubject, but how can I, upon declaration, specify actions that should be done when an observer subscribes, or when the subject gets disposed?
Thanks!
Mostly the disposing part
I tried this:
BehaviorSubject<T> valueSubject = (BehaviorSubject<T>) BehaviorSubject.create(new ObservableOnSubscribe<T>() { @Override public void subscribe(ObservableEmitter<T> emitter) throws Exception { // do something here } });
But I got a io.reactivex.internal.operators.observable.ObservableCreate cannot be cast to io.reactivex.subjects.BehaviorSubject
m0rgan
@m0rgan_twitter
Feb 14 2018 09:16 UTC
I'm feeling it isn't the right approach
m0rgan
@m0rgan_twitter
Feb 14 2018 11:12 UTC
I don't know how to respond to that
David Karnok
@akarnokd
Feb 14 2018 15:07 UTC
Based on your question, you should read a bit about RxJava and Subjects first.
m0rgan
@m0rgan_twitter
Feb 14 2018 15:09 UTC
That is what I have been doing and am currently doing, I just thought I could find some help here, my bad
Thank you anyway
David Karnok
@akarnokd
Feb 14 2018 16:36 UTC
Subjects are self-contained components of RxJava and you can only create them via their own create method. create(ObservableOnSubscribe) is defined on Observable which creates an Observable, a cold source and will have nothing to do with the subject type. Many operators in RxJava are named after the behavior they do in an everyday language, such as "actions done when an observer subscribes" -> doOnSubscribe, when the subscription gets disposed: doOnDispose. Subjects themselves are not disposed.
Based on the misunderstanding your question implies, I wanted to encourage you to familiarize yourself with the concepts and components of RxJava first, so the answer, which invokes more concepts and components, can be understood better. For this, I suggest reading Learning RxJava.
m0rgan
@m0rgan_twitter
Feb 14 2018 17:42 UTC
Thank you David, that helped a lot