2017-07-08 61 views
0

我現在有這個方法,它工作得很好:RxJava2如何使用CONCAT()超過4個觀測

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個觀察對象,我想用concat()來保存,我可能會添加更多。所以我需要一些沒有限制的東西。

+1

做Java的'concat'只允許4個信號源。 – GVillani82

+0

@ GVillani82任何想法我可以做我的情況。我有大約10個觀測值我想串起來。 'getThingOne()'...'getThingTen()'。 – EGHDK

+3

使用'concatArray'。 – akarnokd

回答

1

concat()接受並Iterable<T>與應concated的觀:

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); 
} 

赦免任何語法錯誤,我已經有一段時間沒有