获取最新的其他 observable 的 RxJava zip
RxJava zip that gets latest of other observable
我想实现一个zip
,当其中一个可观察源发出数据时,它不会等待其他可观察源向那里发出数据,而是获取最新发出的数据(如果有的话)。
obs1 --true-|->
obs2 --true-|->
obs3 --1-2-3-4->
zip 应该使用参数
执行 TriFunction
true, true, 1
true, true, 2
true, true, 3
true, true, 4
我希望我的问题有道理。
扩展问题
我已经解决了一部分问题,我还有一个问题要问你们。
obs1
和 obs2
是昂贵的操作,将发出 true
或 false
。我需要的是,在 obs3
的每次发射中,我需要重新执行 obs1
或 obs2
,如果它们中的任何一个在前一次发射中发射 false
。我在顶部写的是 obs1
和 obs2
在 obs3
.
的第一次发射时发射 true
的最佳情况
-------1------------2-----------------3------------->
---true/false---true/re-execute---true/re-execute--->
---true/false---true/re-execute---true/re-execute--->
Edit "either" 在扩展问题上具有误导性。
我的意思是,如果 obs1
在之前的发射中为假,则重新执行 obs1
。如果 obs2
在上一次发射时为假,则重新执行 obs2
。如果其中之一是 false
.
,则不重新执行它们
编辑:扩展问题是一个完全不同的问题,需要不同的运算符,例如:
Observable<Boolean> obs1 = ...
Observable<Boolean> obs2 = ...
Observable<Integer> obs3 = ...
Function3<Integer, Boolean, Boolean> func = ...
// store last result of obs1 and obs2
boolean[] lastResults = { false, false };
// for each main value
obs3.concatMap(v -> {
// if any of the previous results were false
if (!lastResults[0] || !lastResults[1]) {
// run both obs1 and obs2 again
return Observable.zip(obs1, obs2, (a, b) -> {
// save their latest results
lastResult[0] = a;
lastResult[1] = b;
// apply the function to get the output
return func(v, a, b);
});
}
// otherwise call the function with true
return Observable.just(func.apply(v, true, true));
})
.subscribe(...);
我想实现一个zip
,当其中一个可观察源发出数据时,它不会等待其他可观察源向那里发出数据,而是获取最新发出的数据(如果有的话)。
obs1 --true-|->
obs2 --true-|->
obs3 --1-2-3-4->
zip 应该使用参数
执行TriFunction
true, true, 1
true, true, 2
true, true, 3
true, true, 4
我希望我的问题有道理。
扩展问题
我已经解决了一部分问题,我还有一个问题要问你们。
obs1
和 obs2
是昂贵的操作,将发出 true
或 false
。我需要的是,在 obs3
的每次发射中,我需要重新执行 obs1
或 obs2
,如果它们中的任何一个在前一次发射中发射 false
。我在顶部写的是 obs1
和 obs2
在 obs3
.
true
的最佳情况
-------1------------2-----------------3------------->
---true/false---true/re-execute---true/re-execute--->
---true/false---true/re-execute---true/re-execute--->
Edit "either" 在扩展问题上具有误导性。
我的意思是,如果 obs1
在之前的发射中为假,则重新执行 obs1
。如果 obs2
在上一次发射时为假,则重新执行 obs2
。如果其中之一是 false
.
编辑:扩展问题是一个完全不同的问题,需要不同的运算符,例如:
Observable<Boolean> obs1 = ...
Observable<Boolean> obs2 = ...
Observable<Integer> obs3 = ...
Function3<Integer, Boolean, Boolean> func = ...
// store last result of obs1 and obs2
boolean[] lastResults = { false, false };
// for each main value
obs3.concatMap(v -> {
// if any of the previous results were false
if (!lastResults[0] || !lastResults[1]) {
// run both obs1 and obs2 again
return Observable.zip(obs1, obs2, (a, b) -> {
// save their latest results
lastResult[0] = a;
lastResult[1] = b;
// apply the function to get the output
return func(v, a, b);
});
}
// otherwise call the function with true
return Observable.just(func.apply(v, true, true));
})
.subscribe(...);