如何从Flowable获取数据到另一个Flowable?
How to get data from Flowable to another Flowable?
我有 2 个 Flowables(一个给我 VelocityNed
物品,另一个我写来消耗第一个的物品);问题是我不知道如何使第二个正确,因为我仍然不确定 RxJava
我的 Flowable 代码:
private Flowable<Float> getIAS(Flowable<VelocityNed> velocityNed) {
Flowable<Float> flowable = Flowable.create(emitter->{
velocityNed.subscribeWith(new DisposableSubscriber<VelocityNed>() {
@Override public void onNext(VelocityNed v) {
float valueToEmit = (float)Math.sqrt(Math.pow(v.getDownMS(),2)+Math.pow(v.getEastMS(),2)+Math.pow(v.getNorthMS(),2));
//how to emit this
}
@Override public void onError(Throwable t) {
t.printStackTrace();
}
@Override public void onComplete() {
emitter.onComplete();
this.dispose();
}
});
}, BackpressureStrategy.BUFFER);
return flowable;
}
您不需要手动创建 Flowable
来转换发射。你可以做 originalFlowable.map(element -> { transform element however you want })
.
在你的情况下它会是这样的:
Flowable<Float> flowable = velocityNed.map(v -> {
(float)Math.sqrt(Math.pow(v.getDownMS(),2)+Math.pow(v.getEastMS(),2)+Math.pow(v.getNorthMS(),2));
})
我有 2 个 Flowables(一个给我 VelocityNed
物品,另一个我写来消耗第一个的物品);问题是我不知道如何使第二个正确,因为我仍然不确定 RxJava
我的 Flowable 代码:
private Flowable<Float> getIAS(Flowable<VelocityNed> velocityNed) {
Flowable<Float> flowable = Flowable.create(emitter->{
velocityNed.subscribeWith(new DisposableSubscriber<VelocityNed>() {
@Override public void onNext(VelocityNed v) {
float valueToEmit = (float)Math.sqrt(Math.pow(v.getDownMS(),2)+Math.pow(v.getEastMS(),2)+Math.pow(v.getNorthMS(),2));
//how to emit this
}
@Override public void onError(Throwable t) {
t.printStackTrace();
}
@Override public void onComplete() {
emitter.onComplete();
this.dispose();
}
});
}, BackpressureStrategy.BUFFER);
return flowable;
}
您不需要手动创建 Flowable
来转换发射。你可以做 originalFlowable.map(element -> { transform element however you want })
.
在你的情况下它会是这样的:
Flowable<Float> flowable = velocityNed.map(v -> {
(float)Math.sqrt(Math.pow(v.getDownMS(),2)+Math.pow(v.getEastMS(),2)+Math.pow(v.getNorthMS(),2));
})