如何使用rxJava提出乘法请求

时间:2019-07-10 10:10:45

标签: java android rx-java

如何通过改型提出10个并行请求。请帮我解决这个问题。我尝试使用zip运算符,但是只能在Java中使用Function9。

public Single<List<Response<MyResponse>>> getRequest(...) {

        return Single.zip(getNyRequest(sessionId, RequestParams.getParams( "")),
                getNyRequest(sessionId, RequestParams.getParams( "")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                (result1, result2, result3, result4, result5, result6, result7, result8, result9, result10) -> {

                });
}

1 个答案:

答案 0 :(得分:1)

您可以使用可迭代:

  List<Single<String>> singles = Arrays.asList(Single.just("1"), Single.just("2"), Single.just("3"));
  Single<List<String>> zip = Single.zip(singles, objects -> Stream.of(objects).map(o -> (String) o).collect(Collectors.toList()));

在您的情况下:

 List<Single<Response<MyResponse>>> singles = Arrays.asList(getNyRequest(sessionId, RequestParams
        .getParams("")), getNyRequest(sessionId, RequestParams.getParams("")), ....);
 Single<List<Response<MyResponse>>> zip = Single.zip(singles,
        objects -> Stream.of(objects).map(o -> (Response<MyResponse>) o).collect(Collectors.toList()));

已编辑: 要每10分钟重复一次:

getRequest(...).repeatWhen(c -> c.delay(10, TimeUnit.MINUTES))
.subscribe();