JakeWharton / RxReplayingShare

An RxJava transformer which combines replay(1), publish(), and refCount() operators.
Apache License 2.0
628 stars 28 forks source link

PublishSubject does not emmit latest item #17

Closed chriswiesner closed 7 years ago

chriswiesner commented 7 years ago

I've got a PublishSubject which emmits items when the State of a Service changes.

stateSubject = PublishSubject.create()
stateSubject.compose(ReplayingShare.instance());
void onStateChange(State newState) {
   stateSubject.onNext(newState);
}

in my Views (Presenters) i subscribe on that subject and as the Views get destroyed / attach again i want to have the latest state when subscribing again. (--> ReplayingShare)

interactor.getServiceStateObservable()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(countdownServiceState -> {
                    onStateUpdate(state);
                })
                .subscribe();

but it don't get any value when subscribing again.

Did I got anything wrong?

JakeWharton commented 7 years ago

It looks like you aren't storing the result of calling compose() and exposing that Observable to the presenters. You call compose() but ignore the return value that has the new behavior you want.

On Sun, Jan 15, 2017, 2:59 AM chriswiesner notifications@github.com wrote:

I've got a PublishSubject which emmits items when the State of a Service changes.

stateSubject = PublishSubject.create() stateSubject.compose(ReplayingShare.instance());

void onStateChange(State newState) { stateSubject.onNext(newState); }

in my Views (Presenters) i subscribe on that subject and as the Views get destroyed / attach again i want to have the latest state when subscribing again. (--> ReplayingShare)

interactor.getServiceStateObservable() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnNext(countdownServiceState -> { onStateUpdate(state); }) .subscribe();

but it don't get any value when subscribing again.

Did I got anything wrong?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/JakeWharton/RxReplayingShare/issues/17, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEY9LpCRzDPJX8HSAxUwLmsAfJghSks5rSfwQgaJpZM4Lj4vl .

chriswiesner commented 7 years ago

ah right, sorry. at first i tried

stateSubject = PublishSubject.create().compose(ReplayingShare.instance());

but it's not possible to chain it like that, so i "accidently" just split it.

chriswiesner commented 7 years ago

now i got it. I tried to apply compose() on the Subject and reassign it, but compose() returns an Observable so that's not possible here.

there's the BehaviorSubject which is actually doing what i was looking for. I haven't used Subjects before and therefore tried to apply the ReplayingShare first. should have checked the Documentation first.