Search code examples
androidobservablerx-java2rxandroidble

Should Observable call .onComplete() when Subscriber disposes?


I'm writing an Observable to handle connections to Android Services, modeled after RxAndroidBle's .establishConnection().

I know that .establishConnection() never calls .onComplete(); after it emits the connection it is either disposed by the Subscriber or ends with an error (typically if the connection is lost). But it seems reasonable that .onComplete() would be called when the connection is disposed. I haven't been able to find an official RxJava policy on this; it seems that some Observables do it and others don't. What is the correct behavior for an Observable that emits a single item and is then disposed?


Solution

  • What is the correct behavior for an Observable that emits a single item and is then disposed?

    If I understood correctly this is a question about how the Observable should behave when disposed according to the official reactive policy. Actually both calling/not calling the .onComplete() when disposed is aligned to the Observable's contract but the latter seems to be preferred (bolding mine):

    Observable Termination

    An Observable may begin issuing notifications to an observer immediately after the Observable receives a Subscribe notification from the observer.

    When an observer issues an Unsubscribe notification to an Observable, the Observable will attempt to stop issuing notifications to the observer. It is not guaranteed, however, that the Observable will issue no notifications to the observer after an observer issues it an Unsubscribe notification.

    When an Observable issues an OnError or OnComplete notification to its observers, this ends the subscription. Observers do not need to issue an Unsubscribe notification to end subscriptions that are ended by the Observable in this way.

    Where notifications are:

    To Observer:

    • OnNext
    • OnCompleted
    • OnError
    • OnSubscribe (optional)

    To ObservableSource:

    • Subscribe
    • Unsubscribe
    • Request (optional)