简体   繁体   English

我什么时候应该删除 android 上的 livedata 观察者?

[英]When should i remove livedata observer on android?

I am observing a list in firestore using LiveData .我正在使用LiveData观察 firestore 中的列表。 This observations is dependent on another authentication LiveData .此观察结果取决于另一个身份验证LiveData

Should i remove the old LiveData observer before creating the new one?在创建新观察者之前,我应该删除旧的LiveData观察者吗? What will happen if i don't?如果我不这样做会发生什么?

Currently i am removing the observer using next code but i can simplify it greatly if i won't need to since i do the same all over my code目前我正在使用下一个代码删除观察者,但如果我不需要,我可以大大简化它,因为我在我的代码中都做同样的事情

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
...

//Authentication observer which is the ItemAuto dependent
viewModel.auth.observe(viewLifecycleOwner, Observer {auth ->
            updateUserItemAutoLiveData(auth)
        })
}


private fun updateUserItemAutoLiveData(auth: Auth) {
        if (!auth.uid.isNullOrEmpty()) {

            removeUserItemAutoObservers()

            itemAutoLiveDate = viewModel.getUserItemAutoLiveData(auth.uid)

            itemAutoLiveDate!!.observe(viewLifecycleOwner, Observer {
                if (it != null) {
                    if (it.data != null) {
                        itemAutoCompleteAdapter.submitItemAuto(it)
                    } 
                }
            })

        } else {
            removeUserItemAutoObservers()
        }
    }

private fun removeUserItemAutoObservers() {
    if (itemAutoLiveDate != null && itemAutoLiveDate!!.hasObservers()) {
        itemAutoLiveDate!!.removeObservers(this)
    }
} 

ps: i am using Doug Stevenson tutorial which is great! ps:我正在使用Doug Stevenson 教程,这很棒!

If you are using observe method, LiveData will be automatically cleared in onDestroy state.如果您使用observe方法, LiveData将在onDestroy 状态下自动清除。

Observers are bound to Lifecycle objects and clean up after themselves when their associated lifecycle is destroyed.观察者绑定到 Lifecycle 对象并在其关联的生命周期被销毁时自行清理。

More information can be found here更多信息可以在这里找到

You need to remove livedata manually only if you use observeForever method.只有在使用observeForever方法时才需要手动删除livedata。 The reason why you need to remove it manually is because when you use observeForever method, you don't specify the lifecycle of it.之所以需要手动移除,是因为在使用observeForever方法时,没有指定它的生命周期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android 片段 livedata 删除观察者不工作 - android fragment livedata remove observer not working 当我更改 LiveData 的观察者时会发生什么 - What happens when i change the observer of a LiveData Android LiveData Observer 未触发 - Android LiveData Observer Not Triggered 何时调用 livedata 观察者? - When to call livedata observer? 当我在 LiveData 观察器中使用 navController 时,Android 导航组件图停止正常工作 - Android Navigation component graph stop working properly when I use navController in LiveData observer LiveData:从观察者 lambda 内部移除观察者 - LiveData: Remove observer from inside Observer lambda 我可以在我的视图模型中创建一个实时数据观察者吗? 或者我应该总是在片段/活动中观察? - can I make a livedata observer in my viewmodel? or should I always observer in fragment/activity? Android 对记录数据进行更新时不会触发片段 LiveData 观察器 - Android Fragment LiveData observer is not triggered when update is done on a record data 未调用Android LiveData的观察者(但使用observForever) - Observer for Android LiveData not called (but it is with observeForever) onPause之后的Android LiveData Observer - Android LiveData Observer after onPause
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM