我在我的项目中遇到了设置MVVM的问题,一切似乎都很顺利,除了我注意到我的LiveData对象的观察器被多次调用。有时同一个LiveData对象更新一次就会被调用4-5次。
主活动上承载了多个片段,所有片段都连接到相同的ViewModel,该ViewModel连接到我的存储库,然后使用Retrofit向服务器发出API请求。
然后,在每个片段中都有单独的观察器,观察ViewModel中相同的LiveData对象的更改。当使用单一片段时,我没有遇到同一更改的多次观察问题,但在将项目分支化为多个片段后,我注意到观察器被频繁调用。
我尝试将观察器全部更改为使用viewLifecycleOwner,但无济于事。
以下是一个片段的示例,它们在观察LiveData方面本质上是相同的,只是它们更新UI的不同部分。
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this, viewModelFactory)
.get(WeatherViewModel::class.java)
bindUI()
}
private fun bindUI() = launch {
val currentWeatherResponse = viewModel.weatherResponse.await()
currentWeatherResponse.observe(viewLifecycleOwner, Observer {
if(it == null) return@Observer
Log.i("CurrentWeatherFragment", "Observed")
val currentWeather = it.currentWeather.first()
updateTemperature(currentWeather.temperature, currentWeather.tempMin, currentWeather.tempMax)
updateWind(currentWeather.windSpeed, currentWeather.windDirName)
updateHumidity(currentWeather.humidity)
})
}
这是每个片段都连接到的ViewModel:
class WeatherViewModel(
private val forecastRepository: IForecastRepository
) : ViewModel() {
val weatherResponse by lazyDeferred {
forecastRepository.getWeatherResponse()
}
}
我的代码库中的天气响应方法:
override suspend fun getWeatherResponse(): LiveData<out DetailedPrediction> {
return withContext(Dispatchers.IO) {
initWeatherData()
println("Get Weather response method")
println(currentWeatherDao.getWeatherResponse().value)
return@withContext currentWeatherDao.getWeatherResponse()
}
}
天气 DAO 接收来自 saveWeatherResponse 方法的数据发布:
private val weatherResponse = MutableLiveData<DetailedPrediction>()
private val radarImages = MutableLiveData<RadarImageList>()
//TODO Return weather response from shared preferences
override fun getWeatherResponse(): LiveData<DetailedPrediction> {
return weatherResponse
}
我希望能够通过这个单一的LiveData事件来更新所有UI元素,目前它可以正常工作,但观察者触发得太频繁了,我实在想不出为什么。
weatherResponse
被更新时,所有订阅该LiveData
的观察者都会被通知是正常的。您是在说每个观察者实例在单个LiveData
更新时会被多次触发吗? - Sanlok Lee