Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sebaslogen/b9cf3683b002777ab99e2f08b29a477c to your computer and use it in GitHub Desktop.
Save sebaslogen/b9cf3683b002777ab99e2f08b29a477c to your computer and use it in GitHub Desktop.
PublishRelay shared with single onSubscribe and onUnSubscribe using RxReplayingShare
PublishRelay<Integer> publishRelay = PublishRelay.create();
Observable<Integer> relayObservable = publishRelay
.doOnSubscribe(() -> {
Log.i("RxExperiments", "ReplayingShare->doOnSubscribe");
// Register to event here...
}).doOnUnsubscribe(() -> {
Log.i("RxExperiments", "ReplayingShare->doOnUnsubscribe");
// Un-register from event here...
})
.compose(ReplayingShare.instance());
Log.i("RxExperiments", "ReplayingShare->observer-1 subscribes");
Subscription subscription1 = relayObservable.subscribe(i -> {
Log.i("RxExperiments", "ReplayingShare->observer-1->onNext with " + i);
});
publishRelay.call(1);
Log.i("RxExperiments", "ReplayingShare->observer-2 subscribes");
Subscription subscription2 = relayObservable.subscribe(i -> {
Log.i("RxExperiments", "ReplayingShare->observer-2->onNext with " + i);
});
publishRelay.call(2);
Log.i("RxExperiments", "ReplayingShare->observer-1 unsubscribes");
subscription1.unsubscribe();
Log.i("RxExperiments", "ReplayingShare->observer-2 unsubscribes");
subscription2.unsubscribe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment