diff --git a/app/src/main/java/keijumt/devandroid/viewmodel/BaseViewModel.kt b/app/src/main/java/keijumt/devandroid/viewmodel/BaseViewModel.kt index 0ff046e..a6e03c5 100644 --- a/app/src/main/java/keijumt/devandroid/viewmodel/BaseViewModel.kt +++ b/app/src/main/java/keijumt/devandroid/viewmodel/BaseViewModel.kt @@ -4,11 +4,16 @@ import android.arch.lifecycle.LifecycleObserver import android.arch.lifecycle.OnLifecycleEvent import android.arch.lifecycle.ViewModel +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.disposables.Disposable /** * ViewModelの基底クラス */ abstract class BaseViewModel : ViewModel(), LifecycleObserver { + + val compositeDisposable = CompositeDisposable() + @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) open fun onCreate() { } @@ -32,4 +37,13 @@ @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) open fun onDestroy() { } + + fun addDisposable(disposable: Disposable) { + compositeDisposable.add(disposable) + } + + override fun onCleared() { + super.onCleared() + compositeDisposable.clear() + } } \ No newline at end of file