为什么必须删除作为observeForever 添加到LiveData 的观察者? [英] Why Observers added as observeForever to LiveData must be removed?

查看:380
本文介绍了为什么必须删除作为observeForever 添加到LiveData 的观察者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 Android LiveData 文档:

您可以使用 observeForever(Observer) 方法注册一个没有关联 LifecycleOwner 对象的观察者.在这种情况下,观察者被视为始终处于活动状态,因此始终会收到有关修改的通知.您可以调用 removeObserver(Observer) 方法删除这些观察者.

You can register an observer without an associated LifecycleOwner object using the observeForever(Observer) method. In this case, the observer is considered to be always active and is therefore always notified about modifications. You can remove these observers calling the removeObserver(Observer) method.

我正在使用 ViewModel 使用 MVVM 架构模式构建应用程序并声明我的 ViewModel 类中的 LiveDatas.在我的 viewModel 中,我将 observeForever 设置为 LiveData:

I'm building an app using MVVM architecture pattern using ViewModel and declaring LiveDatas inside my ViewModel class. At my viewModel I have set a observeForever to a LiveData:

val password by lazy {
    MutableLiveData<String>()
}

init {
    initObservable()
}

private fun initObservable() {
    password.observeForever {
        ...
    }
}

根据我从文档中的理解,每次实例化 ViewModel 的视图(使用前面的代码)被销毁时,我都应该删除观察者,对吗?但是一旦视图被销毁,观察者不应该被销毁(因为 ViewModel 实例在视图中被实例化,也会被销毁)?

From what I understood from the documentation, I should remove the observer everytime the view that instantiates the ViewModel (with the previous code) was destroyed, right? But shouldn't the Observers be destroyed once the view is destroyed (since the ViewModel instance was instantiated in the view and will also be destroyed)?

推荐答案

每次实例化 ViewModel 的视图(使用前面的代码)被销毁时,我都应该移除观察者,对吗?"

如果您使用 observeForever(observer)ViewModel 中观察 LiveData:

If you are obsering a LiveData in ViewModel using observeForever(observer):

  • 您不必担心 View 的生命周期,因为它与 ViewModel 的生命周期不同.ViewModel 应该能够比创建它的 View 更长寿.相反,当不需要 ViewModel 时,框架将调用 onCleared(),因此您应该在此处处理移除观察者.
  • You should not worry about View's lifecycle, because it is different from ViewModel's life. ViewModel should be able to out-live the View that creates it. Instead, the framework will call onCleared() when the ViewModel is not needed, so that's where you should handle removing the observer.

如果您使用 observe(lifecyclerowner,observer)

  • 当生命周期所有者被销毁时,观察者将被框架自动删除.


但是,一旦视图被销毁,观察者不应该被销毁(因为 ViewModel 实例在视图中被实例化并且也会被销毁)?"

这个问题比 Android 更像是 Java 问题.

This question is more of a Java question than Android.

想想被摧毁"是什么意思.当一个 View 或 ViewModel 被 Android 框架销毁时,并不意味着该对象完全从内存中移除.只要有其他对象(例如观察者)引用它们,您的活动和片段就不会被垃圾收集.

Think about what it means by "being destroyed". When a View or ViewModel is destroyed by the Android Framework, it does not mean that the object is completely removed from the memory. Your activities and fragments will not be garbage collected as long as there are other objects (such as observer) that has reference to them.

如果调用observe(activity,observer),那么Android框架可以跟踪activity实例和observer实例之间的连接,因此它可以在想要杀死 activity 时杀死 observer.但是,如果您只是调用 observeForever(observer),Android 框架根本无法判断此观察者属于哪个对象.

If you call observe(activity, observer), then the Android Framework can track the connection between the activity instance and the observer instance, and therefore it can kill observer when it wants to kill activity. However if you simply call observeForever(observer) there is simply no way for Android Framework to tell which object this observer belongs to.

这篇关于为什么必须删除作为observeForever 添加到LiveData 的观察者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆