[英]How I can prevent reloading data second time after configuration change using ViewModel?
Reloading data after every rotation I fetch data in onCreate and observe in onCreateView().每次旋转后重新加载数据我在 onCreate 中获取数据并在 onCreateView() 中观察。 I want to know after rotating the phone(or after configuration changes data is reloaded again as a result I have these logs before rotation
我想知道在旋转手机后(或在配置更改后再次重新加载数据,因此我在旋转前有这些日志
fetchConfig ->observe
and after rotating I have旋转后我有
observe ->fetchConfig ->observe
How I can prevent reloading data second time?如何防止第二次重新加载数据? I have added in fetchConfig()
我在 fetchConfig() 中添加了
if(customerConfigData.value==null) {}
but I am not sure is it the best solution但我不确定这是最好的解决方案
private val viewModel: HomeViewModel by lazyViewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.fetchConfig()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
viewModel.customerConfigData.observe(viewLifecycleOwner, Observer {
Log.i("test","observe")
})
return inflater.inflate(R.layout.fragment_home,container,false)
}
fun fetchConfig() {
Log.i("test","fetchConfig")
uiScope.launch {
val configEndpoint = EnigmaRiverContext.getExposureBaseUrl().append("v1/customer").append(AppConstants.CUSTOMER_UNIT)
.append("businessunit").append(AppConstants.BUSINESS_UNIT).append("defaultConfig").append("?preview=true")
val parsedData = homeRepository.fetchConfig(configEndpoint, GetConfigCall())
customerConfigMutableData.postValue(parsedData)
}
}
我认为一种解决方案是将对fetchConfig()
调用移动到ViewModel
的init
块中
As you can see, your method has a parameter called savedInstanceState: Bundle?
如您所见,您的方法有一个名为
savedInstanceState: Bundle?
的参数savedInstanceState: Bundle?
. . This bundle is able to save the state of the app when the configuration changes.
当配置更改时,此包能够保存应用程序的状态。 So, you can put here any flag you want.
所以,你可以把任何你想要的标志放在这里。 Now, remember that ViewModels are designed to be implemented with a good code base.
现在,请记住,ViewModel 旨在使用良好的代码库来实现。 So, you need to separate the Ui layer from the business layer.
因此,您需要将 Ui 层与业务层分开。 The fetch configuration method should be in another class which doesn't depend on the Android lifecycle.
fetch 配置方法应该在另一个不依赖于 Android 生命周期的类中。 I strongly recommend reading these articles.
我强烈建议阅读这些文章。
https://medium.com/androiddevelopers/viewmodels-persistence-onsaveinstancestate-restoring-ui-state-and-loaders-fc7cc4a6c090
https://medium.com/androiddevelopers/viewmodels-persistence-onsaveinstancestate-restoring-ui-state-and-loaders-fc7cc4a6c090
https://developer.android.com/jetpack/docs/guide
https://developer.android.com/jetpack/docs/guide
In conclusion.综上所述。 Your solution is not the best.
你的解决方案不是最好的。 The best approach is to implement a correct layer for fetching the info in a way that it doesn't depend on the Android lifecycle.
最好的方法是实现一个正确的层,以不依赖于 Android 生命周期的方式获取信息。
I too had similar issue.我也有类似的问题。 I was suggested to try Event wrapper for LiveData, it had solved my problem:)
有人建议我尝试 LiveData 的事件包装器,它解决了我的问题:)
Here's the link: How to stop LiveData event being triggered more than Once这是链接: 如何停止多次触发 LiveData 事件
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.