Dawid Fickenbaum
Dawid Fickenbaum

Reputation: 21

Chaining api calls in RxJava and Retrofit

I have a service for fetching movies:

Single<List<Movie>> fetchAll();

and a service for fetching trailers for each movie:

Single<List<Video>> fetchByMovieId(@Path("movie_id") long movieId);

After fetching all movies I'd like to also fetch their trailers and return a list of objects that composes of a movie and all of it's trailers. What is a proper way to achieve this using RxJava?

Upvotes: 2

Views: 198

Answers (1)

Lucas Ferraz
Lucas Ferraz

Reputation: 4152

ps: You should avoid make lot of requests. Try to do this on demand.

yourClient.fetchAll()
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .toObservable()
          .flatMapIterable(movies -> movies)
          .flatMap(movie -> yourClient.fetchByMovieId(movie.getId()))
          .toList()
          .subscribe()...

Upvotes: 3

Related Questions