doOnDispose() vs doOnUnsubscribe()
See original GitHub issueWhile 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 withSubscription.unsubscribe()
or becauseonComplete()
oronError()
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 usingObservable.doOnDispose()
instead but this one is called only when explicitly terminating an observable chain withDisposable.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:
- Created 7 years ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
See
doFinally
that has been added since the my original comment.In 1.x, the
SafeSubscriber
always calledunsubscribe
on a terminal event and that triggereddoOnUnsubscribe
. In 2.x, the lifecycle is now different and the upstream is considered disposed/cancelled upon receiving a terminal event. Therefore, unless you explicitly calldispose
, thedoOnDispose
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.