Skip to content

Instantly share code, notes, and snippets.

@markchristopherng
Created September 18, 2018 06:58
Show Gist options
  • Save markchristopherng/56e58ea3cc91ddac3bcd42fa94a60b12 to your computer and use it in GitHub Desktop.
Save markchristopherng/56e58ea3cc91ddac3bcd42fa94a60b12 to your computer and use it in GitHub Desktop.
open class BaseViewModel : ViewModel() {
private val disposableList = mutableListOf<Disposable>()
// integration with rx
fun <T> Observable<T>.subscribe(stateLiveData: StateLiveData<T>, successOnFirstValue: Boolean = true) = stateLiveData.post(this, successOnFirstValue).also { disposableList.add(it) }
override fun onCleared() {
disposableList.filter { !it.isDisposed }.forEach { it.dispose() }
disposableList.clear()
super.onCleared()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment