RxJava2如何使用包含4个以上observable的concat()

时间:2017-07-08 04:25:49

标签: rx-java observable rx-java2

我目前有这种方法可以正常使用:

  public static Observable<MyCustomObject> run(Service networkService) {
    return Observable.concat(
        networkService.getThingOne().map(response -> {
          Request request = response.raw().request();
          MyCustomObject case = new MyCustomObject(request);
          return case;
        }),
        networkService.getThingOne().map(response -> {
          Request request = response.raw().request();
          MyCustomObject case = new MyCustomObject(request);
          return case;
        }),
        networkService.getThingOne().map(response -> {
          Request request = response.raw().request();
          MyCustomObject case = new MyCustomObject(request);
          return case;
        }),
        networkService.getThingOne().map(response -> {
          Request request = response.raw().request();
          MyCustomObject case = new MyCustomObject(request);
          return case;
        })
    );
  }

一遍又一遍地观察到同样的情况。如果我在那里添加另一个:

  public static Observable<MyCustomObject> run(Service networkService) {
    return Observable.concat(
        networkService.getThingOne().map(response -> {
          Request request = response.raw().request();
          MyCustomObject case = new MyCustomObject(request);
          return case;
        }),
        networkService.getThingOne().map(response -> {
          Request request = response.raw().request();
          MyCustomObject case = new MyCustomObject(request);
          return case;
        }),
        networkService.getThingOne().map(response -> {
          Request request = response.raw().request();
          MyCustomObject case = new MyCustomObject(request);
          return case;
        }),
        networkService.getThingOne().map(response -> {
          Request request = response.raw().request();
          MyCustomObject case = new MyCustomObject(request);
          return case;
        }),
        networkService.getThingOne().map(response -> {
          Request request = response.raw().request();
          MyCustomObject case = new MyCustomObject(request);
          return case;
        })
    );
  }

然后我在concat()return case下面的红线,但只在case部分。

有人对此有任何想法吗?

编辑:我将问题标题从“为什么不工作”更新为“我怎样才能让这个工作”?我基本上有10个observable,我想坚持使用concat(),我可能会添加更多。所以我需要一些没有限制的东西。

1 个答案:

答案 0 :(得分:1)

concat()接受并Iterable<T>加上应该合并的观察点:

public static Observable<MyCustomObject> run(Service networkService) {
    ArrayList<Observable<MyCustomObject>> observables = new ArrayList<Observable<MyCustomObject>>();

    observables.add(networkService.getThingOne().map(response -> {
            Request request = response.raw().request();
            MyCustomObject case = new MyCustomObject(request);
            return case;
        }));
    observables.add(networkService.getThingOne().map(response -> {
            Request request = response.raw().request();
            MyCustomObject case = new MyCustomObject(request);
            return case;
        }));
    observables.add(networkService.getThingOne().map(response -> {
            Request request = response.raw().request();
            MyCustomObject case = new MyCustomObject(request);
            return case;
        }));
    observables.add(networkService.getThingOne().map(response -> {
            Request request = response.raw().request();
            MyCustomObject case = new MyCustomObject(request);
            return case;
        });
    observables.add(networkService.getThingOne().map(response -> {
            Request request = response.raw().request();
            MyCustomObject case = new MyCustomObject(request);
            return case;
        }));

    return Observable.concat(observables);
}

请原谅任何语法错误,我暂时没有完成Java