Search code examples
androidandroid-livedatamediatorlivedata

When removing a source from MediatorLiveData, does the associated Observer get removed as well?


When removing a source from MediatorLiveData, does the associated Observer get removed as well?

private final MediatorLiveData<Model> liveModel = new MediatorLiveData<>();
private LiveData<ModelEntity> liveData = repository.getModelEntity();

    liveModel.addSource(liveData , new Observer<ModelEntity>() {
        @Override
        public void onChanged(ModelEntity modelEntity) {}
    });

liveModel.removeSource(liveData);

Solution

  • Yes, when you call removeSource the Observer callback is removed.

    Internally it calls:

    mLiveData.removeObserver(this);
    

    You can check the line here, unplug() function is called from removeSource