question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

doOnDispose() vs doOnUnsubscribe()

See original GitHub issue

While migrating some code from 1.2.x to 2.0 I’ve noticed a difference:

  • 1.2.x : Observable.doOnUnsubscribe() is always called whenever an observable chain terminates, either because it’s been explicitly terminated with Subscription.unsubscribe() or because onComplete() or onError() have been called. This is the way I usually used to insert side effects on chain termination.

  • 2.x : there is no Observable.doOnUnsubscribe() so I’ve resorted using Observable.doOnDispose() instead but this one is called only when explicitly terminating an observable chain with Disposable.dispose();

I’m assuming this difference is intended (i.e. not a bug) so how can we ‘insert’ side effects on chain termination with RxJava 2.0?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
akarnokdcommented, Jul 21, 2017

See doFinally that has been added since the my original comment.

2reactions
akarnokdcommented, Nov 5, 2016

In 1.x, the SafeSubscriber always called unsubscribe on a terminal event and that triggered doOnUnsubscribe. In 2.x, the lifecycle is now different and the upstream is considered disposed/cancelled upon receiving a terminal event. Therefore, unless you explicitly call dispose, the doOnDispose won’t execute.

The general way of making sure a resource is disposed on completion, error or cancellation, you should use using, however, me may consider thinner operator that handles all 3 cases at once.

Read more comments on GitHub >

github_iconTop Results From Across the Web

doOnSubscribe() and doOnDispose() - Learning RxJava [Book]
The doOnDispose() operator will perform a specific action when disposal is executed at that point in the Observable chain. We use both operators...
Read more >
When to use doOnTerminate vs doOnUnsubscribe?
In such a scenario both doOnTerminate and doOnUnsubscribe will be called ... Subscription subscribe = Observable.empty() // source terminates immediately .
Read more >
Making RxJava code tidier with doOnSubscribe, doFinally and ...
I'm a big RxJava enthusiast. As an Android developer, I use it because it helps streamline my development by cutting down on a...
Read more >
Do operator - ReactiveX
The doOnTerminate operator registers an Action which will be called just before the resulting Observable terminates, whether normally or with an error. The ......
Read more >
DoExtensions extension - rx library - Dart API - Pub.dev
... onCancel()) → Stream<T>: Invokes the given callback function when the stream subscription is cancelled. Often called doOnUnsubscribe or doOnDispose in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found