【问题标题】:RxJava2: How to wait all Flowables in a list to finish?RxJava2:如何等待列表中的所有 Flowables 完成?
【发布时间】:2018-03-17 11:18:02
【问题描述】:

假设我有一个Flowable 的列表,我想做一些事情直到所有Flowable 都完成,我该怎么做?

示例代码

List<Flowable<Model>> flowableList = getFlowableList();
List<List<Model>> results = blockUntilAllFlowablesAreFinished(flowableList); // is there a similar method?
... // do something with results

【问题讨论】:

    标签: java rx-java2


    【解决方案1】:

    我不确定您为什么要以同步方式思考,但我相信您会通过以下方式实现目标:

    Flowable.combineLatest(flowableList -> {
        // do something with results
    });
    

    当然,除了combineLatest,还有很多运算符可以让你“组合”可流动的,例如zip。在这里选择一个适合你的https://github.com/ReactiveX/RxJava/wiki/Combining-Observables

    【讨论】:

      【解决方案2】:

      我认为你可以使用Flowable.zip()Iterable 作为参数,它会在Iterable 中的所有Flowables 完成时发出。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-09
        • 1970-01-01
        • 2016-05-23
        • 2020-02-22
        • 2015-04-17
        • 2022-11-30
        • 1970-01-01
        • 2011-09-03
        相关资源
        最近更新 更多