在另一个可观察者的行动中包装可观察物

时间:2016-05-16 11:18:41

标签: retrofit rx-java observable retrofit2 reactivex

我刚开始使用ReactiveX和Retrofit, 请考虑以下示例改造示例

@GET
public Observable<ResponseType1> makeFirstCall();

@POST
public Observable<ResponseType2 makeSecondCallWithFirstResponse(@Body ResponseType1 input);

在另一个action1中有observable是一个好主意吗?如下所示

makeFirstCalle().subscribe((responseType1) -> {

    makeSecondCallWithFirstResponse(responseType1).subscribe("second action goes here")

});

1 个答案:

答案 0 :(得分:2)

为什么不使用concatMap或flatMap?

makeFirstCall().concatMap(responseType1 -> makeSecondCallWithFirstResponse(responseType1))
               .subscribe(....)

如果您有其他api电话,可以继续链接。例如

makeFirstCall().concatMap(responseType1 -> makeSecondCallWithFirstResponse(responseType1))
               .concatMap(responseType2 -> makeThirdCallWithSecondResponse(responseType2))
               .subscribe(....)
相关问题