I'm asking this from an Android perspective, but this should apply to RxJava in general.
As a best practice, should my view always dispose of even short lived Completable
, Single
, Maybe
and terminating Observable
Rx types that should terminate in short order, but could be still be executing when the user closes the view? I'm aware that when the Rx chain terminates, it is disposed but this could potentially occur sometime after the view is closed.
For example, a Single
that's performing an HTTP GET. The call will complete, but it may be after the view destroyed, temporarily preventing garbage collection.
And if a CompositeDisposable
is used to collect such Disposable
s in a long-lived view, I would think that care should be taken to clear()
or otherwise remove these Disposable
s regularly to prevent unbounded growth in the size of CompositeDisposable
?
By calling add() or addAll() , you can dispose of them all at once when they're no longer needed. Congratulations!
observable. onComplete() will complete your stream and so fire this event to all subscribers listening for onComplete , you don't need to dispose stream after onComplete (this is done automatically). disposable. dispose() will stop stream and no complete event will be fired.
Single and Completable are new types introduced exclusively at RxJava that represent reduced types of Observable , that have more concise API. Single represent Observable that emit single value or error. Completable represent Observable that emits no value, but only terminal events, either onError or onCompleted.
The Single class represents the single value response. Single observable can only emit either a single successful value or an error. It does not emit onComplete event.
As a best practice, should my view always dispose of even short lived Completable, Single, Maybe and terminating Observable Rx types
If you're sharing the code with others, as a best practice, I am going to say yes you should dispose. While it may seem like extra boiler plate, it will prevent the next developer from trying to hook into the your subscription code and attempt to access components that may no longer exist (ie a Context
after a Fragment
is detached... etc).
And if a CompositeDisposable is used to collect such Disposables in a long-lived view, I would think that care should be taken to clear() or otherwise remove these Disposables regularly to prevent unbounded growth in the size of CompositeDisposable?
I do want to point out that in rxjava2, CompositeDisposable
s have a state; once you call dispose()
, any subsequent Disposable
added will be immediately disposed of. So as a best practice:
CompositeDisposable
at the beginning of the lifecycle (onCreate(...)
etc)dispose()
in onDestroy(...)
because by that point your callback practically has no value and is just holding onto resources. It's a good practice to dispose CompositeDisposable
in onPause
or onDestroy
to avoid such problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With