Android MVVM-如何使LiveData发出其拥有的数据(强制触发观察者) [英] Android MVVM - How to make LiveData emits the data it has (forcing to trigger the observer)
问题描述
我有这个ViewModel
从网络获取列表,我用数据填充RecyclerView
(MyAvailabilityRepository
返回MutableLiveData
,这就是我使用Transformations.switchMap
的原因):
I have this ViewModel
that gets a list from the network and I populate a RecyclerView
with the data (MyAvailabilityRepository
returns a MutableLiveData
, that's why i'm using Transformations.switchMap
):
class MyAvailabilityViewModel : ViewModel() {
private val getListsParams = MutableLiveData<String>()
private val getListsObservable = Transformations.switchMap(getListsParams) {
organizationId -> MyAvailabilityRepository.getSectionedLists(organizationId)
}
fun getListsObservable() : LiveData<Resource<MutableList<SectionedAvailabilityList>>> {
return getListsObservable
}
fun fetchLists(organizationId: String, forceRefresh: Boolean = false) {
if (getListsParams.value == null || forceRefresh) {
getListsParams.value = organizationId
}
}
}
片段的onActivityCreated
:
override fun onActivityCreated(savedInstanceState: Bundle?) {
...
viewModel.getListsObservable().observe(this, Observer { // populate RecyclerView })
viewModel.fetchLists(organizationId)
}
由于getListParams.value
第一次为空,它将设置getListsParams.value = organizationId
并触发switchMap
并调用存储库以从网络获取列表.
Since getListParams.value
is null the first time, it will set getListsParams.value = organizationId
and trigger the switchMap
and call the repository to get the list from the network.
当我想强制刷新(通过刷新")并再次呼叫网络时,可以使用forceRefresh = true
:
When I want to force a refresh (by pull-to-refresh) and call the network again, I can use forceRefresh = true
:
override fun onRefresh() {
viewModel.fetchLists(organizationId, forceRefresh = true)
}
它将设置organizationId
的值并触发转换,然后调用网络.
It will set the value of organizationId
and trigger the Transformations that will then call network.
但是,在我的场景中,我从RecyclerView的适配器中清除了数据.如果在那之后,用户单击一个按钮,我想再次触发观察者,以便我用getListsObservable已经获取的数据重新填充适配器.我不想在这一点上调用forceRefresh,因为我确定我已经有数据了,我只想再次触发观察者,以便用现有数据更新我的UI.由于此时getListParams.value
不为null,所以以后再调用fetchLists(organizationId)
时什么也没有发生.
But, I have a scenario where I clear the data from my RecyclerView's adapter. If after that, the user click a button, I would like to trigger the observer again so that I re-populate the adapter with the data that the getListsObservable has already fetched. I don't want to call forceRefresh on this one cause i'm sure I already have the data and I would just like to trigger the observer again so that my UI is updated with the existing data. Since getListParams.value
is not null at that point, then nothing happens when I call fetchLists(organizationId)
later on.
任何关于如何使用当前设置实现此目标的想法吗?
Any idea of how I could achieve that with my current setup?
推荐答案
是的,洪端给出的答案是完美的. 我只是在这里扩展答案,
Yes, the answer given by Hong Duan is perfect. I am just going to extend that answer here,
更好的方法是具有扩展功能.
The better way of doing is having an extension function.
我的扩展功能如下:
fun <T> MutableLiveData<T>.forceRefresh() {
this.value = this.value
}
呼叫者函数如下所示,
mutableLiveDataObject.forceRefresh()
快乐编码!
这篇关于Android MVVM-如何使LiveData发出其拥有的数据(强制触发观察者)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!