【问题标题】:Wrapping observables within action of another observable在另一个可观察对象的操作中包装可观察对象
【发布时间】:2016-09-12 04:02:44
【问题描述】:

我刚开始使用 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")

});

【问题讨论】:

    标签: retrofit rx-java observable retrofit2 reactivex


    【解决方案1】:

    为什么不使用 concatMap 或 flatMap?

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

    如果您有额外的 api 调用,您可以继续链接。例如

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

    【讨论】:

    • 我必须调用超过 3 个 api 调用,我必须根据之前的响应调用。这是唯一的方法吗?或者有什么选择?
    • 如果您想以您描述的方式链接可观察对象,我看不到任何其他方式。 flatMap 或 concatMap 有什么问题?
    • 我不知道我刚问过。
    猜你喜欢
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多