我的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());
}
});
}