Reputation: 21
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
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