本文介绍了如何根据属性以不同的方式继续 Rx 链?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种基于 Flowable 数据值的方法,我需要以不同的方式继续 RX 链.我的意思是,如果数据对象的 isOnline 属性为真,那么我需要调用 scan(initial,selector) 但如果它为 false 那么我需要调用 scan(selector)
@NotNull公共 Flowable<Data>初始调用(@NotNull Flowable信息,数据初始){返回信息.地图().switchMap(it -> call(Flowable.just(it),initial, it.isOnline));}私有 Flowable<Data>称呼 (可流动的 A一种,数据初始值,布尔值 isOnline){返回 Flowable.combineLatest(一种,乙,(a, b) ->{返回 ....;}).switchMap()///这里基于我需要调用的 Data isOnline 属性///scan(initial, selector) 或 scan(selector) 然后继续.....地图().distinctUntilChanged().toObservable().撰写().撰写().toFlowable(缓冲)} 解决方案
这是您想要的吗?基于 isOnline
,一个 scan
-operator 被应用,有或没有 seed
值.
import io.reactivex.rxjava3.core.BackpressureStrategy;导入 io.reactivex.rxjava3.core.Flowable;导入 io.reactivex.rxjava3.core.FlowableTransformer;导入 org.junit.jupiter.api.Test;类 So65349760 {private static <C extends Data>FlowableTransformer<数据,数据>扫描(布尔值 isOnline,C 初始值){如果(在线){返回上游 ->{返回上游.scan(初始值,(上一个,当前)->{返回新数据();});};} 别的 {返回上游 ->upstream.scan((prev, current) -> new Data());}}@测试无效所以65349760(){Flowablea = Flowable.fromCallable(() -> 1);Flowableb = Flowable.fromCallable(() -> "42");数据种子=新数据();call(a, b, seed, false).test().assertValueCount(1);call(a, b, seed, true).test().assertValueCount(2);}private <A, B, C extends Data>Flowable称呼(可流动的 Aa,可流动的 Bb, C init, boolean isOnline) {返回 Flowable.combineLatest(a, b, (v1, v2) -> 42).switchMap(integer -> Flowable.just(new Data())).compose(scan(isOnline, init)).map(d -> d).distinctUntilChanged().toObservable().toFlowable(BackpressureStrategy.BUFFER);}私有静态类数据 {}}
I have a method where based on Flowable data value I need to continue RX chain different ways .what I mean is that if the isOnline property of data object is true then I need to call scan(initial,selector) but if it is false then I need to call scan(selector)
@NotNull
public Flowable<Data> initialCall(
@NotNull Flowable<Info> info, Data initial) {
return info
.map()
.switchMap(it -> call(Flowable.just(it),initial, it.isOnline));
}
private Flowable<Data> call (
Flowable<A> a,
Data initial, boolean isOnline
) {
return Flowable.combineLatest(
a,
b,
(a, b) -> {
return ....;
})
.switchMap()
///here based on the Data isOnline property I need to call either
///scan(initial, selector) or scan(selector) and then continue....
.map()
.distinctUntilChanged()
.toObservable()
.compose()
.compose()
.toFlowable(BUFFER)
}
解决方案
Is this, what you want? Based on isOnline
a scan
-operator is applied with or without a seed
value.
import io.reactivex.rxjava3.core.BackpressureStrategy;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.FlowableTransformer;
import org.junit.jupiter.api.Test;
class So65349760 {
private static <C extends Data> FlowableTransformer<Data, Data> scan(
Boolean isOnline, C initialValue) {
if (isOnline) {
return upstream -> {
return upstream.scan(
initialValue,
(prev, current) -> {
return new Data();
});
};
} else {
return upstream -> upstream.scan((prev, current) -> new Data());
}
}
@Test
void so65349760() {
Flowable<Integer> a = Flowable.fromCallable(() -> 1);
Flowable<String> b = Flowable.fromCallable(() -> "42");
Data seed = new Data();
call(a, b, seed, false).test().assertValueCount(1);
call(a, b, seed, true).test().assertValueCount(2);
}
private <A, B, C extends Data> Flowable<Data> call(
Flowable<A> a, Flowable<B> b, C init, boolean isOnline) {
return Flowable.combineLatest(a, b, (v1, v2) -> 42)
.switchMap(integer -> Flowable.just(new Data()))
.compose(scan(isOnline, init))
.map(d -> d)
.distinctUntilChanged()
.toObservable()
.toFlowable(BackpressureStrategy.BUFFER);
}
private static class Data {}
}
这篇关于如何根据属性以不同的方式继续 Rx 链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!