Using liveData in viewModel, I observe if any web api response return, but how to remove specific observe with removeObserve method?
class MyViewModel: ViewModel() {
fun buttomSubmit() {
val responseLiveData = webFetch()
responseLiveData.observeForever(
Observe { // define a Observe?
doSomething()
}
)
}
override fun onCleared() {
responseLiveData.removeObserver(observer) // how to correctly remove the observe
super.onCleared()
}
}
First, define your observer and store it
val mObserver: Observer<MyClass> = Observer { obj ->
doSomething(obj)
}
then you can start observing forever with
responseLiveData.observeForever(mObserver)
and then stop
responseLiveData.removeObserver(mObserver)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.