Rx Android 并行组合对同一服务的多个调用
Rx Android combine multiple calls to the same service in parallel
我必须改进一项旧服务,该服务对同一服务进行多次连续调用并将所有结果合并到一个列表中。
假设我们有一个 IDS = [ 1 ,2 3, .. 6] 列表,所以我必须用每个 ID 调用相同的 API(带有改造和 Observables)。
为此我想使用 Rx Android 但我对如何合并结果有疑问。
Integer[] ids = {1, 2, 3};
Observable.from(ids)
.map(id -> mApi.getData(id))
我们可以并行调用并按照相同的顺序合并结果吗?
为了改进时间响应,我的想法是并行执行此操作,但我不知道如何确保结果将按照我们执行服务器调用的相同顺序组合。
我想使用 concat (http://reactivex.io/documentation/operators/concat.html) 但它会等待前一个可观察到的等待。
参见 concatMapEager
及其变体。
Observable.fromArray(1, 2, 3)
.concatMapEager(id ->
Observable.fromCallable(() -> mApi.getData(id))
.subscribeOn(Schedulers.io())
)
.subscribe(...);
我必须改进一项旧服务,该服务对同一服务进行多次连续调用并将所有结果合并到一个列表中。
假设我们有一个 IDS = [ 1 ,2 3, .. 6] 列表,所以我必须用每个 ID 调用相同的 API(带有改造和 Observables)。
为此我想使用 Rx Android 但我对如何合并结果有疑问。
Integer[] ids = {1, 2, 3};
Observable.from(ids)
.map(id -> mApi.getData(id))
我们可以并行调用并按照相同的顺序合并结果吗?
为了改进时间响应,我的想法是并行执行此操作,但我不知道如何确保结果将按照我们执行服务器调用的相同顺序组合。
我想使用 concat (http://reactivex.io/documentation/operators/concat.html) 但它会等待前一个可观察到的等待。
参见 concatMapEager
及其变体。
Observable.fromArray(1, 2, 3)
.concatMapEager(id ->
Observable.fromCallable(() -> mApi.getData(id))
.subscribeOn(Schedulers.io())
)
.subscribe(...);