Skip to content

Instantly share code, notes, and snippets.

@passiondroid
Last active April 6, 2018 11:45
Show Gist options
  • Save passiondroid/6a0cc44e735ae63fb561f349a3bd36f8 to your computer and use it in GitHub Desktop.
Save passiondroid/6a0cc44e735ae63fb561f349a3bd36f8 to your computer and use it in GitHub Desktop.
private void getMovies() {
MoviesService service = retrofit.create(MoviesService.class);
Call<List<Movie>> call = service.getMovies();
call.enqueue(new Callback<List<Movie>>() {
@Override
public void onResponse(Response<List<Movie>> response, Retrofit retrofit) {
saveMovies(response.body())
getMovieCasts(response.body())
}
@Override
public void onFailure(Throwable t) {
Utils.showErrorMessage(StringConstants.ERROR_MESSAGE);
}
});
}
private void getMovieCasts(List<Movie> movies) {
// Note we are doing it on new thread because
// we need to fetch all the casts sequentially
// and update the UI when all the casts are fetched
new Thread(new Runnable() {
@Override
public void run() {
MoviesService service = retrofit.create(MoviesService.class);
for(Movie movie: movies){
Call<List<Cast>> call = service.getCasts(movie.getId());
List<Cast> casts = call.execute();
movie.setCasts(casts);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// Now update UI with movies.
// This way we have both movies and casts together.
updateUI(movies);
}
});
}
}).start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment