问题描述
当前代码
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) {
}
});
在上面的代码中,我将 Movie 对象列表传递给 Observable 并在从 API 获取结果时对列表中的每个电影实例执行操作我想更改有关该电影实例的一些数据库如何在 OnNext 中获取每个 Movie 实例() 以及 subscribe 方法的 onError 方法.我想要的是
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
}
});
推荐答案
我猜您有一个 List 并且想要处理每个收到的项目以进行另一个异步操作.对于这种情况,您可以对每个结果进行平面映射.
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
意味着它将列表中的每个项目拆分为 Observable 到流中的下一个操作.flatMap
表示将对接收到的值进行操作.
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.
如果您想将结果重新组合在一起,您可以在 flatMap 之后使用 toList
.
If you want to put the results back together you can use toList
after flatMap.
您需要为您的平面图创建一个 Observable(操作).
You need to create an Observable (Operation) for your flatmap.
科特林:
Observable.just(data)
.flatMapIterable { it }
.flatMap{ moviesAPI.makeMovieFavObservable(whatEver) }
.subscribe( ... , ... , ... )
Java(未经测试)
Observable.just(data)
//parse each item in the list and return it as observable
.flatMapIterable(d -> d)
// use each item and to another observable operation
.flatMap(data -> Observable.just(moviesAPI.movieStuff(data)))
// use each result and put it back into a list
.toList() // put the result back to a list
// subscribe it and log the result with Tag data, throw the error and output when completed
.subscribe( data -> Log.d("Data", "Data received "+ data),
error -> error.printStackTrace(),
() -> Log.d("Completed", "Completed")
);
这篇关于如何将自定义 pojo 作为参数传递给 rxjava2 Observable 中的接口方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!