Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebaslogen/4d92f030b0bfaae7da49d439a1dfa3f0 to your computer and use it in GitHub Desktop.
Save sebaslogen/4d92f030b0bfaae7da49d439a1dfa3f0 to your computer and use it in GitHub Desktop.
BehaviorRelay shared with multiple observers produces problematic onSubscribe and onUnSubscribe events
BehaviorRelay<Integer> brelay = BehaviorRelay.create();
Observable<Integer> relayObservable = brelay
.doOnSubscribe(() -> {
Log.i("RxExperiments", "BehaviorRelayProblem->doOnSubscribe");
}).doOnUnsubscribe(() -> {
Log.i("RxExperiments", "BehaviorRelayProblem->doOnUnsubscribe");
});
Log.i("RxExperiments", "BehaviorRelayProblem->observer-1 subscribes");
Subscription subscription1 = relayObservable.subscribe(i -> {
Log.i("RxExperiments", "BehaviorRelayProblem->observer-1->onNext with " + i);
});
brelay.call(1);
Log.i("RxExperiments", "BehaviorRelayProblem->observer-2 subscribes");
Subscription subscription2 = relayObservable.subscribe(i -> {
Log.i("RxExperiments", "BehaviorRelayProblem->observer-2->onNext with " + i);
});
brelay.call(2);
Log.i("RxExperiments", "BehaviorRelayProblem->observer-1 unsubscribes");
subscription1.unsubscribe();
Log.i("RxExperiments", "BehaviorRelayProblem->observer-2 unsubscribes");
subscription2.unsubscribe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment