Android MediatorLiveData observer

旧巷老猫 提交于 2019-12-21 09:22:06

问题


I'm a bit confused on why the following code doesn't work:

MutableLiveData<String> mutableTest = new MutableLiveData<>();
MediatorLiveData<String> mediatorTest = new MediatorLiveData<>();
mediatorTest.addSource(mutableTest, test -> {
    Timber.d(test);
});
mutableTest.setValue("bla!");

This code seems straightforward, however the debugger doesn't enter the callback and nothing is logged to the console...

Edit: shouldn't this work then?

    MutableLiveData<String> mutableTest = new MutableLiveData<>();
    MediatorLiveData<String> mediatorTest = new MediatorLiveData<>();
    mediatorTest.observe(loginActivity, str -> Timber.d(str));
    mediatorTest.addSource(mutableTest, str -> Timber.d(str));
    mutableTest.setValue("bla!");

回答1:


This answer is largely reproduction of what @CommonsWare has already shared in the comment section above.

In order for the callback on MediatorLiveData's addSource method to be triggered, the MediatorLiveData object needs to be observed itself as well.

The logic behind this is that the 'mediator' mediates between a LiveData object that it observes, and a final consumer of the data. The mediator is hence an observer and observable simultaneously, and the callback on addSource won't be triggered for the mediator when there are no active observers.

As an example; according to Google's Android Architecture Components, an activity or fragment could have an observer observing a mediator on the ViewModel, which in turn may observe other LiveData objects that are handled within the ViewModel or a referenced to an utility class.

@CommonsWare pointed out the use of the Transformation class that exposes methods map and switchMap, but these were not within scope of my use case although they are worth checking out.




回答2:


I got here since I had more or less the same experience, but instead with MediatorLiveData.getValue(). I wasn't aware that was a problem until I faced it big time. My problem can be stated like this:

MutableLiveData<String> mutableTest = new MutableLiveData<>();
MediatorLiveData<String> mediatorTest = new MediatorLiveData<>();
mediatorTest.addSource(mutableTest, test -> {
    mediatorTest.value = test;
});
mutableTest.setValue("bla!");
mediatorTest.getValue(); // will be null

I know it's a bit simplified, but nevertheless MediatorLiveData.getValue() will not contain "bla" and in that way you never really know if you can trust getValue() unless you're 100% sure it is active (has more than one oberserver).

Same issue is the case for Transformations.map(...) and TransformationsswitchMap(...), where getValue() of the returned LiveData doesn't necessarily returns the newest value unless it's observed.



来源:https://stackoverflow.com/questions/45679896/android-mediatorlivedata-observer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!