[英]RxJava: How to .zip two Observable, then .merge them and eventually .reduce to aggregate all results
I have the following code: 我有以下代码:
public void foo() {
Long[] gData = new Long[] { 1L, 2L };
rx.Observable.from(gData)
.concatMap(data -> {
rx.Observable<GmObject> depositObs1 = depositToUserBalance(data, 1);
rx.Observable<GmObject> depositObs2 = depositToUserBalance(data, 2);
return rx.Observable.zip(depositObs1, depositObs2, (depositObj1, depositObj2) -> {
depositObj1.putNumber("seat_index", data);
depositObj2.putNumber("seat_index", data);
return rx.Observable.merge(
rx.Observable.just(depositObj1),
rx.Observable.just(depositObj2));
})
})
.reduce(new ArrayList<Long>(), (payoutArr, payoutObj) -> {
int seatIndex = ((GmObject) payoutObj).getNumber("seat_index").intValue();
long payout = ((GmObject) payoutObj).getNumber("payout").longValue();
payoutArr.add(seatIndex, payout);
return payoutArr;
})
.subscribe(results -> {
System.out.println(results);
});
}
This code uses .zip to emits to observables, and then it adds a 'seat_index' property and calls .merge in order to use .reduce so eventually all results would be aggregated into an ArrayList. 此代码使用.zip发送到observable,然后添加'seat_index'属性并调用.merge以使用.reduce,因此最终所有结果将聚合到ArrayList中。
There is a problem with this code: When .reduce processes its input it gets it as Observable and not as GmObject ...What function can 'extract' the GmObject from its Observable wrap? 这段代码有一个问题:当.reduce处理它的输入时,它将它作为Observable而不是GmObject ...什么函数可以从它的Observable包中“提取”GmObject?
Does it make sense to use rxJava this way? 以这种方式使用rxJava是否有意义? or there is a better technique?
还是有更好的技术?
Thanks! 谢谢!
the zip
operator take as third argument a lambda. zip
运算符将lambda作为第三个参数。 this lambda is a 2 args function which return an object that result of the composition of args. 这个lambda是一个2 args函数,它返回一个args组成的对象。 And not a
Observable
of the result of the composition (but, of course, the object can be an Observable
, but it's not what you want in your case). 而不是组成结果的
Observable
(但是,当然,对象可以是Observable
,但它不是你想要的)。
So after your zip
call, you'll have an Observable<Observable<GmObject>>
but you expect an Observable<GmObject>
. 所以在你的
zip
调用之后,你将有一个Observable<Observable<GmObject>>
但你期望一个Observable<GmObject>
。
I don't think that the zip
operator is the operator you're looking for. 我不认为
zip
运算符是您正在寻找的运算符。
public void foo() {
Long[] gData = new Long[] { 1L, 2L };
rx.Observable.from(gData)
.concatMap(data -> {
rx.Observable<GmObject> depositObs1 = depositToUserBalance(data, 1).doOnNext(obj -> obj.putNumber("seat_index", data));
rx.Observable<GmObject> depositObs2 = depositToUserBalance(data, 2).doOnNext(obj -> obj.putNumber("seat_index", data));
return rx.Observable.merge(depositObs1, depositObs2);
})
.reduce(new ArrayList<Long>(), (payoutArr, payoutObj) -> {
int seatIndex = ((GmObject) payoutObj).getNumber("seat_index").intValue();
long payout = ((GmObject) payoutObj).getNumber("payout").longValue();
payoutArr.add(seatIndex, payout);
return payoutArr;
})
.subscribe(results -> System.out.println(results));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.