0

I'm sure I read somewhere that .isDisposed() is not guaranteed to return true unless .dispose() has been explicitly called. But this answer says that it always returns true after .onComplete() has been called, while this one says that .doOnDispose() is never called after a completion event.

I'd like to create an Action that runs if the Observable is disposed, but not if it completes normally. Is there any way to guarantee this? What is the official answer on the semantics of .isDisposed()? The possibility that it might or might not return true after completion isn't very satisfactory.

Update: I'm trying to convert a Single into a 1-item Observable. Once it emits its item, it will of course become completed. But I would still like its .dispose() method to trigger a separate action:

        mObservable = mSingle
                .toObservable()
                .doOnDispose( () -> {
                    do stuff} );

The whole reason I am converting the Single to an Observable is to have the ability to handle the doOnDispose() action. Am I correct that a Single is torn down after emitting its item, so therefore its doOnDispose method is no longer available? (If I'm wrong about this I can skip the conversion to an Observable)

Robert Lewis
  • 1,847
  • 2
  • 18
  • 43
  • `doOnDispose` runs only when the sequence is disposed. The lambda- based `subscribe` methods return a `Disposable` that reports disposed upon termination. – akarnokd Oct 02 '18 at 15:10
  • And I presume that if I create my own `Observer` object I can handle this any way I choose? – Robert Lewis Oct 02 '18 at 15:14
  • What do you want to do? Call `Disposable.dispose()` from `Observer.onComplete()`? – akarnokd Oct 02 '18 at 15:20
  • I've edited the question to explain more fully – Robert Lewis Oct 02 '18 at 15:38
  • `doOnDispose` may not execute if the sequence has terminated, depending on what chain you have. Some operators disconnect themselves from the upstream so `dispose()` may not propagate beyond them after a terminal event. It makes no sense to run `doOnDispose` in that case as the operators themselves are responsible for cleanup. What is that you want to execute specifically upon dispose? – akarnokd Oct 02 '18 at 16:02
  • In this case I want to subscribe to a different `Single` (which queues an async call with `OkHttp3` and emits the response) – Robert Lewis Oct 02 '18 at 16:55

1 Answers1

0

@akarnokd has pointed out something I never fully understood: when you use lambdas in a subscribe() call, under the hood a LambdaSubscriber is created, and it reports isDisposed() true on termination.

If you don't want this, you must evidently write your own Observer with the desired behavior. AFAIK there is no way to swap out the default LambdaSubscriber.

Robert Lewis
  • 1,847
  • 2
  • 18
  • 43