Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Observers added as observeForever to LiveData must be removed?

I've read on Android LiveData documentation that:

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.

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 {
        ...
    }
}

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)?

like image 657
Ana Paula Avatar asked Apr 29 '19 20:04

Ana Paula


People also ask

How do I remove all observers from LiveData?

This call is similar to observe(LifecycleOwner, Observer) with a LifecycleOwner, which is always active. This means that the given observer will receive all events and will never be automatically removed. You should manually call removeObserver(Observer) to stop observing this LiveData.

Why LiveData Observer is being triggered twice for a newly attached observer?

You set your observers within the onCreateView() hook method. After this point the lifecycle changes it state twice to come visible, on start and on resume .

Can LiveData have multiple observers?

LiveData is amazing android architectural component for most of use cases where we need to fetch data from some data source.

How do you use ObserveForever?

To use ObserveForever, you need to remove the observer inside onClear in the ViewModel. In this case, I would suggest to use Transformation even though you just need a direct mapping without any processing of the data, which is actually the same as what you are doing with the observer for observerForever.


1 Answers

"I should remove the observer everytime the view that instantiates the ViewModel (with the previous code) was destroyed, right?"

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

  • 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.

If you are observing a LiveData in View using observe(lifecyclerowner, observer)

  • Observers will automatically removed by the framework when the lifecycleowner is destroyed.


"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)?"

This question is more of a Java question than 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.

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.

like image 88
Sanlok Lee Avatar answered Nov 09 '22 23:11

Sanlok Lee