Why is disposing DisposableObserver is important in this case
The reason behind using dispose method is because after the system initiate the view (activity
or fragment
), the subscription gets start and then you have decided to go back or initiate another view while the older subscription is still getting executed and didn't finish its job. This means that it's still in the memory which will cause a memory leak
. So you have to call dispose method for unsubscribe.
Adding more to @abozaid's answer, When older subscription is still On
and in the meantime, our user switches to other view (activity
or fragment
) or closes older view (or application
itself), it'll definitely leak
memory.
But, if we were observing observable for UI updation with AndroidSchedulers.mainThread()
scheduler, then our code would crash because at the time of updating UI, the view
and context
would have gone away (or destroyed).
myObservable.observeOn(AndroidSchedulers.mainThread()) // like this
One other point, I can add here is that, even if we handle the crash by putting precaution in code, the subscription
running unused would hamper performance
at some stage.