Feature Request P3
Status Update
Comments
se...@google.com <se...@google.com> #2
We'll try to come up with something in 2.2.0-*. From theoretical point of view "removeAllSources" is a bit dangerous, because MediatorLiveData can be "shared", so if there are multiple clients that did "addSource" and then one of them called "removeAllSources" it may be destructive and surprising behavior for other clients. However, from practical point of view, I understand your argument that it is fairly inconvenient to keep references manually.
m....@gmail.com <m....@gmail.com> #3
This didn't seem to make it in version 2.2.0.
Any plan for 2.3.0? Or no news so far on this?
Any plan for 2.3.0? Or no news so far on this?
Description
Right now, code that uses a MediatorLiveData not only needs the MediatorLiveData but needs to keep track of all sources added to it, if you ever want to remove those sources.
A less dramatic change would be to add a removeAllSources() or clear() method to MediatorLiveData that removes all outstanding sources, reverting the MediatorLiveData to its original state.
Scenario:
You have a ViewModel that is supporting an MVI-style app, so the ViewModel has a LiveData<ViewState>, where the ViewState represents an immutable set of data to populate in the UI (e.g., via data binding). The source for that LiveData, though, varies based on user input (search strings, filter criteria, etc.), so the repository exposes a function that returns a LiveData<Model> based on that input criteria. We want the fragment to have a consistent source of truth for the UI data, so we want the LiveData<ViewState> to be the same LiveData instance throughout the fragment's life. So, we make it be a MediatorLiveData<ViewState>, where we use addSource() to attach the LiveData<Model> and use the observer lambda to map the Model to the ViewState. When the user changes the input data, we can call addSource() for the new LiveData<Model> that we get from the repository... but we also need to remove the earlier source. Right now, that means we have to hold onto the earlier LiveData<Model> to be able to call removeSource(). Ideally, we could just call removeAllSources() instead.
If you elected to offer getSources() as per
Thanks for considering this!