RxJava - 组合两个Observable

时间:2016-09-05 16:41:54

标签: java android retrofit rx-java rx-android

我需要将来自其他Retrofit请求的图片合并到其特定系列中。 听起来像一个简单的任务让我深深陷入了被动的地狱,没有希望的迹象。

具体来说,我还通过Observable创建服务,因为我需要获取身份验证令牌来创建服务。

我根据这个答案进行了尝试:https://stackoverflow.com/a/28418503/2192545

public void loadSeries(String searchFor) {

    mainView.showLoadingIndicator();
    if (subscription != null) subscription.unsubscribe();


    TVApplication application = TVApplication .get(mainView.getContext());

    application.getTVService()
            .flatMap(service -> service.searchSeries(searchFor)
                    .flatMap(series -> Arrays.asList(series).stream()
                            .<List<Series>>flatMap(aSeries -> {
                                Observable<Series> obsSeries = Observable.just(aSeries);
                                Observable<ImageListWrapper> obsImages = service.queryImages(aSeries.getId(), "poster");
                                return Observable.zip(obsSeries, obsImages, (Series s, ImageListWrapper i) -> combineSeriesData(s, i));
                            })));
}

private Series combineSeriesData(Series s, ImageListWrapper i) {
    s.setPosters(i.getImages());
    return s;
}

我在我头上。我刚刚得到&#34;无法推断功能界面类型&#34;在IDE中的Observable.zip的Func2部分,构建日志说:

Error:(63, 58) error: incompatible types: bad return type in lambda 

expression
no instance(s) of type variable(s) T1,T2,R exist so that Observable<R> conforms to Stream<? extends List<Series>>
where T1,T2,R are type-variables:
T1 extends Object declared in method <T1,T2,R>zip(Observable<? extends T1>,Observable<? extends T2>,Func2<? super T1,? super T2,? extends R>)
T2 extends Object declared in method <T1,T2,R>zip(Observable<? extends T1>,Observable<? extends T2>,Func2<? super T1,? super T2,? extends R>)
R extends Object declared in method <T1,T2,R>zip(Observable<? extends T1>,Observable<? extends T2>,Func2<? super T1,? super T2,? extends R>)

1 个答案:

答案 0 :(得分:2)

好的,首先要做的事情;不要将服务创建为Observable,它会使您的代码更难。有办法解决这个问题。

其次,您不需要使用zip:

application
  .getTVService()
  .flatMap(service ->
     service
     .searchSeries(searchFor)
     .flatMapIterable(series -> series)
     .flatMap(aSeries -> 
        service
        .queryImages(aSeries.getId(), "poster")
        .map(i ->  combineSeriesData(aSeries, i))
     )
  );