将MediatorliveData与多个源一起使用的最佳做法是什么?
我在viewmodel中有一个MediatorliveData,可以从最终显示的数据视图中访问.
MediatorliveData依赖于多个其他liveDatas.其中一些来自存储库层,其中一些必须在viewmodel中处理,然后才能从MediatorliveData访问它们,其中一些来自VIEw.
所以我当前的实现看起来像以下架构:
public Myviewmodel extends viewmodel { liveData<Foo> liveData1; liveData<bar> liveData2; liveData<Foobar> liveData3; //Some other liveDatas MediatorliveData liveDataForVIEw public Myviewmodel() { liveDataForVIEw = new MediatorliveData(); //Do some preprocessing with some of the liveData setupForVIEw(); } public MediatorliveData getliveDataForVIEw() { return liveDataForVIEw; } private voID setupForVIEw() { liveDataForVIEw.addSource(liveData1, (foo -> { if(liveData1.getValue() != null && liveData2.getValue() != null && liveData3.getValue() != null /*&& some other liveData-checks*/) liveDataForVIEw.setValue(/*Some combinations of the liveDatas*/); })); //Add sources to the MediatorliveData for any other liveData }}
通过这个实现我声明,输出liveData的值是在每个liveData出现后设置的.
在某些情况下,如果我留下一些空检查,我得到一个NullPointerException.
但是这个解决方案对我来说似乎有些混乱,因为对于我必须添加到viewmodel的每个liveData,我必须将它添加到每个源代码中.
解决方法:
首先你需要一些元组:
public class Tuple2<S, T> { public final S first; public final T second; public Tuple2(S first, T second) { this.first = first; this.second = second; }}
和
public class Tuple3<S, T, U> { public final S first; public final T second; public final U third; public Tuple3(S first, T second, U third) { this.first = first; this.second = second; this.third = third; }}
和
public class Tuple4<S, T, U, V> { public final S first; public final T second; public final U third; public final V fourth; public Tuple4(S first, T second, U third, V fourth) { this.first = first; this.second = second; this.third = third; this.fourth = fourth; }}
一旦你有了元组,就可以编写类似于transformations.map的辅助函数,除非你现在可以这样做:
public class liveDatatransformations { private liveDatatransformations() {} public static <S, T> liveData<Tuple2<S,T>> ifNotNull(liveData<S> first, liveData<T> second) { MediatorliveData<Tuple2<S, T>> mediator = new MediatorliveData<>(); mediator.addSource(first, (_first) -> { T _second = second.getValue(); if(_first != null && _second != null) { mediator.setValue(new Tuple2(_first, _second)); } }); mediator.addSource(second, (_second) -> { S _first = first.getValue(); if(_first != null && _second != null) { mediator.setValue(new Tuple2(_first, _second)); } }); return mediator; } public static <S, T, U> liveData<Tuple3<S,T,U>> ifNotNull(liveData<S> first, liveData<T> second, liveData<U> third) { MediatorliveData<Tuple3<S, T, U>> mediator = new MediatorliveData<>(); mediator.addSource(first, (_first) -> { T _second = second.getValue(); U _third = third.getValue(); if(_first != null && _second != null && _third != null) { mediator.setValue(new Tuple3(_first, _second, _third)); } }); mediator.addSource(second, (_second) -> { S _first = first.getValue(); U _third = third.getValue(); if(_first != null && _second != null && _third != null) { mediator.setValue(new Tuple3(_first, _second, _third)); } }); mediator.addSource(third, (_third) -> { S _first = first.getValue(); T _second = second.getValue(); if(_first != null && _second != null && _third != null) { mediator.setValue(new Tuple3(_first, _second, _third)); } }); return mediator; } public static <S, T, U, V> liveData<Tuple4<S,T,U, V>> ifNotNull(liveData<S> first, liveData<T> second, liveData<U> third, liveData<V> fourth) { MediatorliveData<Tuple4<S, T, U, V>> mediator = new MediatorliveData<>(); mediator.addSource(first, (_first) -> { T _second = second.getValue(); U _third = third.getValue(); V _fourth = fourth.getValue(); if(_first != null && _second != null && _third != null && _fourth != null) { mediator.setValue(new Tuple4(_first, _second, _third, _fourth)); } }); mediator.addSource(second, (_second) -> { S _first = first.getValue(); U _third = third.getValue(); V _fourth = fourth.getValue(); if(_first != null && _second != null && _third != null && _fourth != null) { mediator.setValue(new Tuple4(_first, _second, _third, _fourth)); } }); mediator.addSource(third, (_third) -> { S _first = first.getValue(); T _second = second.getValue(); V _fourth = fourth.getValue(); if(_first != null && _second != null && _third != null && _fourth != null) { mediator.setValue(new Tuple4(_first, _second, _third, _fourth)); } }); mediator.addSource(fourth, (_fourth) -> { S _first = first.getValue(); T _second = second.getValue(); U _third = third.getValue(); if(_first != null && _second != null && _third != null && _fourth != null) { mediator.setValue(new Tuple4(_first, _second, _third, _fourth)); } }); return mediator; }}
现在你可以做到
liveData<???> liveDataForVIEw;private voID setupForVIEw() { liveData<Tuple3<Foo, bar, Foobar>> intermediate = liveDatatransformations.ifNotNull(liveData1, liveData2, liveData3); liveDataForVIEw = transformations.map(intermediate, (tuple) -> { Foo foo = tuple.first; bar bar = tuple.second; Foobar foobar = tuple.third; return /*Some combinations of the liveDatas*/ });}
总结 以上是内存溢出为你收集整理的android – 仅通过当前数据使用MediatorLiveData的最佳实践全部内容,希望文章能够帮你解决android – 仅通过当前数据使用MediatorLiveData的最佳实践所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)