justmee
justmee

Reputation: 355

RXJava Single after unsubscribe(), doAfterTerminate() not called?

I have the following code:

Single<Item> update(...) {
...
return Single.create(subscriber -> aCall.execute(..) {
public void onResponse(..){
subscriber.onSuccess(..)
}
public void onError() {
  if(shouldReset) {
    subscriber.unsubscribe();
  } else {
    subscriber.onError();
  }
}
}));
    }

When calling the method:

update(..) 
.doOnSubscribe(counter++)
.doAfterTerminate(counter--)
.subscribe();

I've noticed that the counter is never decreased if subscriber.unsubscribe(); is called. Why is that?

If I change from doAfterTerminate() in doOnUnsubscribe(), the counter is decreased.

Upvotes: 1

Views: 1112

Answers (1)

elmorabea
elmorabea

Reputation: 3263

doAfterTerminate() will work in the case of termination due to an onError or an onComplete, but not unsubscription.

You can either use doFinally or combine doAfterTerminate() with doOnCancel to get the call also when unsubscribed.

Refer to this answer for an explanation of the difference.

Upvotes: 4

Related Questions