I try to apply the MVVM pattern in my Android activity (I'm an Android noob).
I use Room with RxJava 2, e.g. this is a signature of a method in my repository:
public Single<MissionTask> getMissionTaskByID(long id) {..}
In my ViewModel class I have a reference to the repository and code like this:
private void doSomethingOnUserEvent() {
...
missionTaskRepository.getMissionTaskByID(firstID).
observeOn(AndroidSchedulers.mainThread()).
subscribeOn(Schedulers.io()).
subscribe(missionTask ->
{
// do some work and update live data
},
t -> {
// handle error
});
...
}
So far so good, everything seems to work fine on the surface. Now - subscribe
returns a Disposable
.
My questions are:
In some of the examples I've gone through there is no handling of the Disposable.
Update: I've seen the usage of composite disposable in android-architecture-components.
Thanks.
Just clear your disposable/composite disposible in onCleared
, this is enough
protected override onCleared(){
if( diposable != null )
disposable.dispose()
}
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