You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've come across a couple of differences in the subscription behavior of Observable, Single, and Completable that I was hoping I could get clarification on or bring up for discussion. The examples specifically revolve around converting an observable to those types and how its events are propagated.
In a normal Observable, an onCompleted() event automatically unsubscribes.
This still holds true in Single, but with the added caveat that an onCompleted event actually propagates to onError() if no event has been emitted prior for onSuccess(). This would be sort of ok considering the Single contract, but things get a little muddled in the sense that onSuccess() does not actually unsubscribe despite being considered a terminal event (or so I thought). What this means is that onCompleted() has to be called manually after onSuccess()
PublishSubject<String> stringSubject = PublishSubject.create();
Singlesingle = stringSubject.toSingle();
SubscriptionsingleSubscription = single.subscribe();
stringSubject.onNext("This is necessary");
stringSubject.onCompleted(); // This is necessary tooassertTrue(singleSubscription.isUnsubscribed());
Things get more confusing in Completable, which offers no auto-unsubscribe after onComplete() is called as far as I can tell.
This would imply that you always need to save the subscription and manually unsubscribe in onComplete() or doOnComplete().
PublishSubject<String> stringSubject = PublishSubject.create();
Completablecompletable = stringSubject.toCompletable();
finalCompositeSubscriptionset = newCompositeSubscription();
set.add(completable
.doOnComplete(newAction0() {
@Overridepublicvoidcall() {
// Kludgeset.unsubscribe();
}
})
.subscribe());
stringSubject.onCompleted();
assertTrue(set.isUnsubscribed()); // Now it works
I'm not sure if this behavior still holds true when dealing with "pure" Single and Completable subscriptions, I can investigate more if need be.
It does seem inconsistent to me, or at the very least prone to causing passive leaks or unexpected behavior due to subscriptions living on past "terminal" events. Maybe some clarification is needed on what constitutes a "terminal" event in Single and Completable. Would love to get some more insight from the collaborators that have worked on these.
The text was updated successfully, but these errors were encountered:
Completable follows the Reactive-Streams lifecycle where if one receives a terminal event, the associated Subscription should be considered cancelled. Those Completable operators which hold onto resources should do exactly this so there is no need to call unsubscribe(). CompletableSubscriber also doesn't have an add(Subscription) method so you can't just associate it with resources which would require cleanup like with rx.Subscriber.
I've come across a couple of differences in the subscription behavior of
Observable
,Single
, andCompletable
that I was hoping I could get clarification on or bring up for discussion. The examples specifically revolve around converting an observable to those types and how its events are propagated.In a normal
Observable
, anonCompleted()
event automatically unsubscribes.This still holds true in
Single
, but with the added caveat that anonCompleted
event actually propagates toonError()
if no event has been emitted prior foronSuccess()
. This would be sort of ok considering theSingle
contract, but things get a little muddled in the sense thatonSuccess()
does not actually unsubscribe despite being considered a terminal event (or so I thought). What this means is thatonCompleted()
has to be called manually afteronSuccess()
Things get more confusing in
Completable
, which offers no auto-unsubscribe afteronComplete()
is called as far as I can tell.This would imply that you always need to save the subscription and manually unsubscribe in
onComplete()
ordoOnComplete()
.I'm not sure if this behavior still holds true when dealing with "pure"
Single
andCompletable
subscriptions, I can investigate more if need be.It does seem inconsistent to me, or at the very least prone to causing passive leaks or unexpected behavior due to subscriptions living on past "terminal" events. Maybe some clarification is needed on what constitutes a "terminal" event in
Single
andCompletable
. Would love to get some more insight from the collaborators that have worked on these.The text was updated successfully, but these errors were encountered: