简体   繁体   English

RxJava-结合两个Observable

[英]RxJava - Combine two Observables

I need to merge Images from another Retrofit request to its specific series. 我需要将图像从另一个改造请求合并到其特定系列。 What sounds like an easy task has brought me deep into reactive hell with no sign of hope. 听起来很简单的任务使我陷入了被动的地狱,没有希望的迹象。

Specifically, I also create the Service via an Observable since I need to obtain an authentication token to create the service. 具体来说,我还通过Observable创建服务,因为我需要获取身份验证令牌才能创建服务。

I based my attempts on this answer: https://stackoverflow.com/a/28418503/2192545 . 我基于以下答案进行尝试: 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;
}

I'm in way over my head. 我在头上。 I just get "Cannot infer functional interface type" on the Func2 part of Observable.zip in the IDE, the build log says: 我只是在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>)

Ok, first things first; 好,首先 don't create the Service as an Observable, it will make your code harder. 不要将Service创建为Observable,这会使您的代码更难。 There are ways around that. 有一些解决方法。

Secondly, you don't need to use zip: 其次,您不需要使用zip:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM