简体   繁体   English

使用 ViewModel 更改配置后如何防止第二次重新加载数据?

[英]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()调用移动到ViewModelinit块中

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.

相关问题 使用viewmodel在SQLite中更改数据后如何获取新数据? - How to get new data after data change in SQLite using viewmodel? 使用 MVVM 视图模型和实时数据时,如何在配置更改后保留 RecyclerView 的滚动位置? - How to preserve scroll position of a RecyclerView after a configuration change when using MVVM viewmodel and livedata? Android:如何防止方向更改后重新加载活动? - Android: How to prevent activity from reloading after orientation change? ViewModel如何在配置更改时保留 - How ViewModel is persisted on configuration change ViewModel 如何在配置更改中幸存下来 - How ViewModel survives configuration change 如何在 Android 中每 5 秒更改一次图像 - How can I change image after every 5 second in Android 如何更改android中的viewmodel可观察数据? - How do I change the viewmodel observable data in android? 如何更改 kotlin 中的实时数据(字符串)? 您正在使用视图模型 - How to change live data(String) in kotlin? You are using a viewmodel 如何在ViewModel中更改xml对象的可见性? - How can I change the visibility of an xml object in ViewModel? 如何使用LiveData和ViewModel类将数据从Activity发送到Fragment - How can i send data from Activity to Fragment using LiveData and ViewModel class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM