对于包含 List<B> 的 List<A> ,如何使用 RxJava2 更改 B 中的值和 return 修改后的 List<A> ?
For a List<A> containing List<B> , How do I change the values inside B and return the modified List<A> using RxJava2?
我正在尝试以 MVP 模式实现 rxandroid,
这是我的用例:
A.class
List<B> b;
List<B> getB(){
return b;
}
B.class
String dataTochange;
void setDataToChange(String dataToChange){
this.dataToChange = dataToChange
}
我想 return 相同 Observable(List (A)) 更改 B 中的值后
这就是我所做的,
REMOTEDATASOURCE.java:
Observable<List<A>> getAList(){
return FromApiService
.getAList() // returns List<A>
.flatMapIterable(List<A> -> A )
.map(A::getB)
.flatMapIterable(List<B> b -> b)
.map(b -> doSomeDownloading(b))
//After this i want to return the modified
List<A> containing list of B with dataChanged.
}
doSomeDownloading(B b){
somValue = doAsynchronousDownload(b)
b.setDataToChange(somValue);
}
doAsynchronousDownload(B b){
contains callbacks to listeners implemented by View for progress etc;
}
存储库的结构:
PS: 实现逻辑是否正确,应该在presenter中下载。或者在存储库中!
更新
serviceApi.getAList()
.flatMapIterable(List<A> -> a)
.flatMap(A a ->
Observable.fromIterable(a.getB())
.doOnNext(b ->doSomeDownloading(b))
.toList()
.toObservable()
.map(new Function<List<B>, A>() {
@Override
public A apply(List<B> b) throws Exception
{
a.setB(b);
return a;
}
})
)
.toList()
.toObservable()
.doOnSubscribe(...)
上面的操作显示,doSomeDownloading() 的结果被忽略 ..
这实际上取决于您如何将 A 转换为 List 以及您将如何处理它。
如果您的转换很简单(意味着它们不是 time/resource 消耗过程)。您可以只使用 flatMap 来 "cache" 您的值
例如:
FromApiService.getAList()
.flatMapIterable(List<A> -> A)
.flatMap{a:A ->
Observable.fromIterable(a.getB())
.doOnNext{b -> doSomeDownloading(b)}//or Map/FlatMap if you want
.toList()
.toObservable()//here you have your new list<B>
.map{b -> a.setB(b)}//here you get your A
}
我正在尝试以 MVP 模式实现 rxandroid, 这是我的用例:
A.class
List<B> b;
List<B> getB(){
return b;
}
B.class
String dataTochange;
void setDataToChange(String dataToChange){
this.dataToChange = dataToChange
}
我想 return 相同 Observable(List (A)) 更改 B 中的值后
这就是我所做的,
REMOTEDATASOURCE.java:
Observable<List<A>> getAList(){
return FromApiService
.getAList() // returns List<A>
.flatMapIterable(List<A> -> A )
.map(A::getB)
.flatMapIterable(List<B> b -> b)
.map(b -> doSomeDownloading(b))
//After this i want to return the modified
List<A> containing list of B with dataChanged.
}
doSomeDownloading(B b){
somValue = doAsynchronousDownload(b)
b.setDataToChange(somValue);
}
doAsynchronousDownload(B b){
contains callbacks to listeners implemented by View for progress etc;
}
存储库的结构:
PS: 实现逻辑是否正确,应该在presenter中下载。或者在存储库中!
更新
serviceApi.getAList()
.flatMapIterable(List<A> -> a)
.flatMap(A a ->
Observable.fromIterable(a.getB())
.doOnNext(b ->doSomeDownloading(b))
.toList()
.toObservable()
.map(new Function<List<B>, A>() {
@Override
public A apply(List<B> b) throws Exception
{
a.setB(b);
return a;
}
})
)
.toList()
.toObservable()
.doOnSubscribe(...)
上面的操作显示,doSomeDownloading() 的结果被忽略 ..
这实际上取决于您如何将 A 转换为 List 以及您将如何处理它。 如果您的转换很简单(意味着它们不是 time/resource 消耗过程)。您可以只使用 flatMap 来 "cache" 您的值 例如:
FromApiService.getAList()
.flatMapIterable(List<A> -> A)
.flatMap{a:A ->
Observable.fromIterable(a.getB())
.doOnNext{b -> doSomeDownloading(b)}//or Map/FlatMap if you want
.toList()
.toObservable()//here you have your new list<B>
.map{b -> a.setB(b)}//here you get your A
}