【发布时间】:2020-08-18 05:16:13
【问题描述】:
我的 viewModel 重新获取我的数据,导致我的微光在每次我到达这个片段时继续执行,并且数据被重新获取,这使得我在 Firebase 上的账单增加了
如何防止每次我的片段弹出时再次重新获取数据?
在我看来
viewModel.fetchShops(location).observe(viewLifecycleOwner, Observer {
when(it){
is Resource.Loading -> {
shimmer.visibility = View.VISIBLE
shimmer.startShimmer()}
is Resource.Success -> {
shimmer.visibility = View.GONE
shimmer.stopShimmer()
adapter.setItems(it.data)
}
is Resource.Failure -> {
Toast.makeText(requireContext(),"Error fetching data",Toast.LENGTH_SHORT).show()
}
}
})
我在我的onViewCreated() 中调用它,所以每次这个片段重新创建我的Resource.Loading 和fetchShops(location) 并再次获取到我的数据库时,我希望它每次返回时只获取一次这个片段,有什么想法吗?
视图模型
fun fetchShops(location:String) = liveData(Dispatchers.IO) {
emit(Resource.Loading())
try{
emit(repo.fetchShops(location))
}catch (e:Exception){
emit(Resource.Failure(e))
}
}
【问题讨论】:
-
fetchShops(location)是做什么的? -
@ianhanniballake 更新了我的问题
标签: android firebase android-fragments kotlin