MutableLiveData未正常更新状态:加载状态无法切换问题
问题:提交Loading状态后UI状态无法更新
我在从服务器下载数据时,尝试通过postValue提交Loading状态来向用户显示进度条,代码如下:
private fun loadBottomSheetItems(currentViewState: BusinessMapViewState.Display, getBusinessByIdsRequest: GetBusinessByIdsRequest) { viewModelScope.launch { _businessMapViewState.postValue( currentViewState.copy( bottomSheetState = BottomSheetViewState.Loading <--------------- 始终保持该状态! ) ) val responseFlow = businessRepository.getBusinessListByIds( getBusinessByIdsRequest ) responseFlow.collect { result -> if (result.isSuccess()) { val businesses = result.asSuccess().value.businessList _businessMapViewState.postValue( currentViewState.copy( bottomSheetState = BottomSheetViewState.Display( items = businesses.map { business -> BusinessListCardItemModel( businessId = business.id, businessName = business.name ) } ) ) ) } else { _businessMapViewState.postValue( currentViewState.copy( bottomSheetState = BottomSheetViewState.Error ) ) } } } }
提交Loading状态后,数据加载完成该状态却无法改变。如果移除Loading状态的postValue代码块,或添加延迟,数据就能正常显示,但我需要显示进度条。我还尝试将Loading状态的postValue代码块移到viewModelScope外,依旧没有变化。
更新
我已解决问题,是代码的其他部分修改了UI状态 🙈
内容的提问来源于stack exchange,提问作者Ilya Shutov
相关产品推荐
相关产品推荐

