完成后从平面图中获得两个可观察对象的结果

时间:2018-09-24 22:07:56

标签: java android rx-java observable flatmap

我有两个使用RxJava和Retrofit的可观察对象,以及一个带有平面图的可观察对象

1)(tmdbApi.discoverymovies)调用一个api并从一组特定的参数器中获取电影列表

2)(addImdbIdToMovieList)将电影列表作为参数,并返回添加了imdbID的电影列表。

我想在电影上运行第三个功能,但是我找不到放置它的位置。在电影具有与其关联的ImdbId之前,我无法运行它。如果可能的话,我想在每部电影上运行它,但是不要多次运行某些列表项,但是如果需要,我可以等到列表完成。

我应该在哪里拨打包含电影列表并对其执行操作的呼叫?

    private void getMovies(Params params) {
    listOMovies = new ArrayList<>();
        runTheOtherTwoObservables(params).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<ArrayList<Movie>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ArrayList<Movie> movieArrayList) {
            listOMovies = movieArrayList;
            }

            @Override
            public void onError(Throwable e) {
                Log.e("zzzzz" , "err", e);
            }

            @Override
            public void onComplete() {
                Log.d("zzzzz", "running oncomplete");
                for (Movie movie : listOMovies) {
                    Log.d("zzzzz", movie.getTitle() + ": " + movie.getImdbID());
                }

            }
        });
}

private TmdbApi getTmdbApi() {
    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("https://api.themoviedb.org")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create());

    Retrofit retrofit = builder.build();
    return retrofit.create(TmdbApi.class);
}

private Observable<ArrayList<Movie>> addImdbIdToMovieList(ArrayList<Movie> listOfMoviesWithoutIds) {
    TmdbApi tmdbApi = getTmdbApi();

    return Observable.create(emitter -> {

        for (Movie movie : listOfMoviesWithoutIds) {
            tmdbApi.TMDBdetails(String.valueOf(movie.getTmdbID()), getResources().getString(R.string.tmdb_key), "en-us")
                    .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Movie>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(Movie movie) {
                            String imdbID = movie.getImdbID();
                            movie.setImdbID(imdbID);
                            Log.d("zzzzz", movie.getTitle() + " Set To " + movie.getImdbID());
                        }

                        @Override
                        public void onError(Throwable e) {
                            Log.e("zzzzz", "err", e);
                        }

                        @Override
                        public void onComplete() {
                            Log.d("zzzzz", "thrrd onComplete");
                        }
                    });
        }
        emitter.onNext(listOfMoviesWithoutIds);
        emitter.onComplete();
    });
}
public Observable<ArrayList<Movie>> runTheOtherTwoObservables(Params params) {
   TmdbApi tmdbApi = getTmdbApi();
    return tmdbApi.discoveryMovies(getResources().getString(R.string.tmdb_key),
            "en-us",
            "popularity.desc",
            params.getInclude_adult(),
            false,
            params.getPrimary_release_date_min(),
            params.getPrimary_release_date_max(),
            params.getWith_genres())
            .flatMap(response -> addImdbIdToMovieList(response.getResults()));
}

0 个答案:

没有答案