Observable fromIterable只返回一项

时间:2019-02-08 11:01:49

标签: android observable rx-java iterable

我的observable.fromIterable仅返回1次。

我正在尝试返回可观察对象之类的元素列表。为此,我使用“ fromIterable”。 topMoviesRated.getResults()返回20个对象,doOnNext被调用20次,但是getResultFromNetwork方法仅返回第一个元素。

演示者:

public void loadData(boolean error) {
    subscription = model.result(error)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeWith(new DisposableObserver<MyFilm>() {
        @Override
        public void onNext(MyFilm object) {
            if(view!=null){
                view.updateData(object);
            }
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
            if(view!=null){
                view.showSnackbar("Error...");
            }
        }

        @Override
        public void onComplete() {
            if(view!=null){
                view.showSnackbar("Finished...");
            }
        }
    });
}

型号:

public Observable<MyFilm> result(boolean error) {
    if (error)
        repository.setPagination(1);
    else
        repository.setPagination(++pagination);

    return Observable.zip(repository.getResultFromNetwork(), detailResult(),  new BiFunction<Result, Details, MyFilm>() {
                @Override
                public MyFilm apply(Result result, Details details) {
                    return new MyFilm(result.getOriginalName(), result.getId().toString(), details);
                }
            });
}

public Observable<Details> detailResult() {
    return Observable.zip(repository.getDetailsData(), repository.getCountryData(), new BiFunction<TVDetails, String, Details>() {
        @Override
        public Details apply(TVDetails tvDetails, String country) {
            return new Details(tvDetails, country);
        }
    });
}

存储库:

public Observable<Result> getResultFromNetwork() {
    Observable<TopMoviesRated> topMoviesRatedObservable = moviesApiService.getTopMovies("es_ES", pagination);
    return topMoviesRatedObservable.concatMap(new Function<TopMoviesRated, Observable<Result>>() {
        @Override
        public Observable<Result> apply(TopMoviesRated topMoviesRated) {
            return Observable.fromIterable(topMoviesRated.getResults());
        }
    }).doOnNext(new Consumer<Result>() { 
        @Override
        public void accept(Result result) {
            Log.d("OnNext -->", result.toString());
        }
    });
}

2 个答案:

答案 0 :(得分:1)

检出此图像。第一个列表有5个项目,第二个只有4个。如您所见,zip运算符的结果仅包含4个项目。zip operator

答案 1 :(得分:0)

Yamko是正确的,zip不是您所需要的。尝试使用Observable.combineLatest代替