观察函数返回的实时数据

问题描述 投票:0回答:0

如果我在函数中通过

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 ->
             ...
         }
android mvvm viewmodel android-livedata android-mvvm
© www.soinside.com 2019 - 2024. All rights reserved.