如果我在函数中通过
liveData
返回请求并在活动中观察它以显示响应,我的活动是否总是观察 liveData
或仅在调用函数时观察?
viewModel:
fun blockContacts(contactList: List<Contact>): LiveData<Resource<Any>> {
val live = MutableLiveData<Resource<Any>>(Resource.loading())
viewModelScope.launch(ioDispatcher) {
mutex.withLock {
...
}
}
return live
}
活动:
viewModel.blockContacts(checkedList).observe(this) { res ->
...
}