在Rxjava中使用flatMap进行并行调用

时间:2018-07-06 16:56:01

标签: android rx-java2

public interface ASarTaLineApi {

@FormUrlEncoded
@POST("GetWarDee.php")
Observable<GetWardeeResponse> getWardee(@Field("access_token") String access_token);

@FormUrlEncoded
@POST("GetMealShop.php")
Observable<GetMealShopResponse> getMealShop(@Field("access_token") String access_token);}

如何在RxJava中与flatMap并行调用。我想同时使用两个Object。请详细回答我,我不了解kotlin。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用zipWith进行这样的并行操作:

String token = "hello";
api.getMealShop(token)
        .zipWith(api.getWardee(token), new BiFunction<GetMealShopResponse, GetWardeeResponse, Pair<GetMealShopResponse, GetWardeeResponse>>() {
            @Override
            public Pair<GetMealShopResponse, GetWardeeResponse> apply(GetMealShopResponse getMealShopResponse, GetWardeeResponse getWardeeResponse) throws Exception {
                return new Pair<>(getMealShopResponse, getWardeeResponse);
            }
        });

或者lambda'd:

api.getMealShop(token)
        .zipWith(api.getWardee(token), Pair::new);

您返回一个Observable<Pair<GetMealShopResponse, GetWardeeResponse>>。如果那不是您想要的,只需在zipper参数中添加其他内容即可。

相关问题