我的代码类似于
val isLoading = Transformations.map(_profileResponse) {
it is Resource.Loading
}
val error: LiveData<String> = Transformations.map(_profileResponse) { result ->
(result as? Resource.Failure)?.message
}
val profile: LiveData<Profile> = Transformations.map(_profileResponse) { result ->
(result as? Resource.Success)?.data
}
fun fetchProfile() {
viewModelScope.launch {
mainRepository.getUserProfile().collectLatest {
_profileResponse.value = it
}
}
fun updateProfile(editableProfile: EditableProfile) {
viewModelScope.launch {
mainRepository.updateUserProfile(editableProfile)
.collectLatest {
_profileResponse.value = it
}
}
}
fetchProfile 获取配置文件,updateProfile 更新配置文件。调用 updateProfile 后,profile 由于 Loading 发出 null,这是预期的。由于我在数据绑定布局中使用该配置文件实例(无法过滤),因此在生成网络调用时这些值变为空。有什么方法可以阻止配置文件实例不发出空值?
提前致谢。