简体   繁体   English

使用 RxJava 同步两个异步 API 调用

[英]Sync two asynchronous API call with RxJava

In what way can we sync two asynchronous calls using RxJava?我们可以通过什么方式使用 RxJava 同步两个异步调用? In the example below, the method contentService.listContents which is a API call must first finish before the processSchema method to take place for each schema.在下面的示例中,作为 API 调用的方法contentService.listContents必须先完成,然后processSchema方法才会为每个模式发生。

schemaService.listSchema()
    .toObservable()
    .flatMapIterable(schemas -> {
        schemas.forEach(schema -> {
            // async call
            contentService.listContents(schema.getName()).subscribe(contents -> {
                   doSomethingWithThe(contents); 
            });
        });
        // contentService.listContents` must complete first before 
        // processSchema should be called for each schema
        return schemas;
    }).subscribe(schema -> { processSchema(schema); }, 
                 error -> { Console.error(error.getMessage()); });

The problem with the code above the processSchema would not wait for the contentService.listContents since it is async not not synchronized with each other. processSchema上方代码的问题不会等待contentService.listContents ,因为它是异步的,不会彼此同步。

You have to use flatMap to process the schemas and since it is a list, you have to unroll it and flatMap again:您必须使用flatMap来处理schemas ,因为它是一个列表,您必须再次展开它和flatMap

schemaService.listSchema()
.toObservable()
.flatMap(schemas -> 
     Observable.fromIterable(schemas)
     .flatMap(schema -> 
         contentService.listContents(schema.getName())
         .doOnNext(contents -> doSomethingWith(contents))
     )
     // probably you don't care about the inner contents
     .ignoreElements()
     // andThen will switch to this only when the sequence above completes
     .andThen(Observable.just(schemas))
)
.subscribe(
    schema -> processSchema(schema), 
    error -> Console.error(error.getMessage())
);

Note that you haven't defined the return types of the service calls so you may have to use flatMapSingle and doOnSuccess for example.请注意,您尚未定义服务调用的返回类型,因此您可能必须使用flatMapSingledoOnSuccess等示例。

You are probably looking for flatMap .您可能正在寻找flatMap

From the docsdocs

Continuations延续

Sometimes, when an item has become available, one would like to perform some dependent computations on it.有时,当一个项目变得可用时,人们想对其执行一些相关的计算。 This is sometimes called continuations and, depending on what should happen and what types are involved, may involve various operators to accomplish.这有时称为延续,根据应该发生的情况和涉及的类型,可能需要各种运算符来完成。

Dependent依赖

The most typical scenario is to given a value, invoke another service, await and continue with its result:最典型的场景是给定一个值,调用另一个服务,等待并继续其结果:

service.apiCall()
.flatMap(value -> service.anotherApiCall(value))
.flatMap(next -> service.finalCall(next))

It is often the case also that later sequences would require values from earlier mappings.通常情况下,后来的序列也需要来自早期映射的值。 This can be achieved by moving the outer flatMap into the inner parts of the previous flatMap for example:这可以通过将外部 flatMap 移动到先前 flatMap 的内部部分来实现,例如:

service.apiCall()
.flatMap(value ->
    service.anotherApiCall(value)
    .flatMap(next -> service.finalCallBoth(value, next))
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM