【发布时间】:2021-08-22 11:51:09
【问题描述】:
我正在尝试使用 MutableLiveData 和 switchMap() 更新我的列表,即使我将新值发布到 LiveData switchMap() 从未调用过。
这是我如何更新 MutableLiveData 从我的:
viewModel.getProductListingById(queryModelId)
viewModel 中的getProductListingById() 实现:
fun getProductListingById(newQueryModelId: ProductListingQueryModelId) {
queryModelId.postValue(newQueryModelId)
}
这是我如何定义 queryModelId:
private val queryModelId = MutableLiveData<ProductListingQueryModelId>()
最后是switchMap():
val productListingId = queryModelId.switchMap { query ->
//This log statement never called
Log.e("ProductViewModel ","id =${query.id}")
productListingRepo.getProductListingById(query.id.toInt(), query.body).cachedIn(viewModelScope)
}
【问题讨论】:
-
newQueryModelId是一个全新的对象吗?还是已经保存在MutableLiveData中的同一对象,但具有不同的属性?换句话说,尝试构建一个全新的ProductListingQueryModelId,使用所有正确的属性对其进行初始化,然后将其传递给getProductListingById()。 -
是的,它是一个新对象
标签: android kotlin android-livedata android-jetpack android-viewmodel