Current code
Observable.from(listMovie)//list of movie
.flatMap(new Func1<Movie, Observable<FavMovieRes>>() {
@Override
public Observable<FavMovieRes> call(Movie movie) {
return moviesAPI.makeMovieFav(userId),
sessionId, new MakeMovieFav("movie", movie.getId(), movie.isFavList()));
}
})
.subscribe(new Subscriber<FavMovieRes>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(FavMovieRes favMovieRes) {
}
});
In above code I am passing Movie object list to the Observable and perform an operation on each movie instance in list when result gets from the API I want to change some database regarding that movie instance how can I get each Movie instance in OnNext() as well as onError method of subscribe method. what I want is
Observable.from(listMovie)
.flatMap(new Func1<Movie, Observable<FavMovieRes>>() {
@Override
public Observable<FavMovieRes> call(Movie movie) {
return moviesAPI.makeMovieFav(String.valueOf(SharedPreferenceDataManager.getUserId(SyncFavListPeriodicTask.this)), SharedPreferenceDataManager.getSessionId(SyncFavListPeriodicTask.this), new MakeMovieFav("movie", movie.getId(), movie.isFavList()));
}
}).subscribe(new Subscriber<FavMovieRes>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e,Movie movie) {//or MakeMovieFav makeMovieFav
}
@Override
public void onNext(FavMovieRes favMovieRes,Movie movie) {//or MakeMovieFav makeMovieFav
}
});
I guess you have a List and want to process each received item for another single async operation. For this case you can flatmap each result.
flatMapIterable
means that it will split each item in the list as Observable to the next Operation in your Stream.flatMap
means that it will do an operation on the value received.If you want to put the results back together you can use
toList
after flatMap.You need to create an Observable (Operation) for your flatmap.
Kotlin:
Java (Untested)