我试图用MutableLiveData和switchMap()更新我的列表,即使我向LiveData switchMap()发布了新的值,也不会调用这个问题。
下面是如何从我的:
viewModel.getProductListingById(queryModelId)getProductListingById()在viewModel中的实现
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)
}发布于 2021-08-22 12:35:58
最后,我发现了一个问题,您必须确保您在ui中观察到您的LiveData,所以在我的例子中,val productListingId在我的ui中添加了观察者之后,从未像预期的那样工作。
https://stackoverflow.com/questions/68881032
复制相似问题