RxJava zip操作在不同长度的Retrofit Observable数组上

时间:2017-02-23 10:01:22

标签: rx-java observable retrofit2 rx-android

我有一个不同长度的Observable数组。我想压缩请求(即发出一堆API请求并等到所有这些请求完成),但我无法弄清楚如何实现zip函数。

ts1 <- structure(c(15.5, 18.74, 22.75, 25.9, 25.43, 27.61, 27.6, 27.72, 
27.63, 24.38, 20.34, 17.74, 17.9, 20.57, 23.13, 25.6, 26.41, 
26.98, 25.8, 26.19, 24.99, 23.23, 19.59, 15.78, 14.85, 18.97, 
20.44, 25.78, 26.65, 27, 26.25, 26.97, 25.33, 23.16, 20.47, 15.47, 
15.64, 18.33, 22.71, 26.71, 25.77, 25.94), .Tsp = c(1951.5, 1954.91666666667, 
 12), class = "ts")


v1 <- c(27.80, 27.24, 26.14, 23.76, 20.19, 16.87, 16.71, 19.28, 22.74, 25.23,
 26.5, 27.2) 

此处Observable.zip(observables, new FuncN<List<ResponseBody>>() { @Override public List<ResponseBody> call(Object... args) { return Arrays.asList(args); <- compile error here } }); 是一个obserables的数组,其长度未知是先验的。

zip函数中的调用参数无法更正为List<Observable<ResponseBody>>。如何让它返回ResponseBody...

它是Observable<List<ResponseBody>> RxJava 1.x.x设计中的约束吗?

P.S。我正在使用 RxJava 1.1.6

2 个答案:

答案 0 :(得分:0)

我确认merge运算符有效并找到导致顺序的罪魁祸首:1st request, 1st response, 2nd request, 2nd response, 3rd request, 3rd response

observables列表中的观察结果在添加期间未在Scheduler.io()订阅。

此前:

observables.add(mViewModelDelegate.get().getApiService()
                    .rxGetCategoryBrandList(baseUrl, categoryId));

后:

observables.add(mViewModelDelegate.get().getApiService()
                    .rxGetCategoryBrandList(baseUrl, categoryId)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());

答案 1 :(得分:0)

用户合并并确保所有可观察请求都有自己的线程。 你可以试试这个:

private void runMyTest() {
    List<Single<String>> singleObservableList = new ArrayList<>();
    singleObservableList.add(getSingleObservable(500, "AAA"));
    singleObservableList.add(getSingleObservable(300, "BBB"));
    singleObservableList.add(getSingleObservable(100, "CCC"));
    Single.merge(singleObservableList)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(System.out::println);
}

private Single<String> getSingleObservable(long waitMilliSeconds, String name) {
    return Single
            .create((SingleOnSubscribe<String>) e -> {
                    try {
                        Thread.sleep(waitMilliSeconds);
                    } catch (InterruptedException exception) {
                        exception.printStackTrace();
                    }
                    System.out.println("name = " +name+ ", waitMilliSeconds = " +waitMilliSeconds+ ", thread name = " +Thread.currentThread().getName()+ ", id =" +Thread.currentThread().getId());
                    if(!e.isDisposed()) e.onSuccess(name);
                })
            .subscribeOn(Schedulers.io());
}

输出:

  

System.out:name = CCC,waitMilliSeconds = 100,thread name =   RxCachedThreadScheduler-4,id = 463

     

System.out:CCC

     

System.out:name = BBB,waitMilliSeconds = 300,thread name =   RxCachedThreadScheduler-3,id = 462

     

System.out:BBB

     

System.out:name = AAA,waitMilliSeconds = 500,thread name =   RxCachedThreadScheduler-2,id = 461

     

System.out:AAA