0

所以我有一个场景,我想结合两个 flowables 的最新结果并用它做一些事情。

Flowable.combineLatest(
                info,
                list,
                BiFunction  {  ... }
        )

在某些情况下,我需要能够再次获得结果,并做一些与以前不同的事情。所以我可以在某处手动存储 combinelatest 的结果,然后重复使用它们,但我在想,也许有一种方法可以添加第三个 flowable,并手动触发 onNext,以便再次传播结果。这可能吗?

4

1 回答 1

1

There are two approaches to keeping the computed value around for later use. You can create a BehaviorSubject that acts as an intermediate variable, that when defined will have the computed value, or you can publish() the observable so that newer subscribers will get the most recent results.

BehaviorSubject intermediateResult = BehaviorSubject.create();

Flowable.combineLatest(info, list, ...)
  .subscribe( intermediateResult );

Alternatively,

Observable<Type> intermediateResult = Flowable.combineLatest(info, list, ...)
  .replay(1)
  .publish();

In either case, subscribing to intermediateResult will get the most recent computed value, if it is present.

Edit: make the function selectable on the fly:

Observable<FunctionSelector> fnSelector;

Observable<Type> intermediateResult = 
  Flowable.combineLatest(info, list, fnSelector, 
     (information, listToUse, selector) -> 
       getFunction(selector).apply(information, listToUse))
  .replay(1)
  .publish(1);
于 2017-10-20T17:53:07.670 回答