我有2个改装电话,我需要制作A& B:
(A):返回一个ArrayList
(B):得到(A)的结果,它是一个ArrayList。 (B)遍历ArrayList并使用each来进行改进调用,并将结果数据组合成最终的ArrayList,发送给我的订阅者onNext()
我能够使用flatmap,但解决方案不是很优雅。据我了解,更好的解决方案是将flatMapIterable与concatMap一起使用,但我似乎无法采用我使用concatMap处理flatMapIterable的方法。
ServiceFactory.createRetrofitService().getUserFollowing(sessionMgr.getAuthToken())
.flatMap(new Func1<UserSelfFollows, Observable<? extends ArrayList<Media.MediaData>>>() {
@Override
public Observable<? extends ArrayList<Media.MediaData>> call(UserSelfFollows userSelfFollows) {
//make second requests based on response from First request to get all Users
ArrayList<Media.MediaData> arAllMedia = new ArrayList<>();
for(UserSelfFollows.UserDataFollows user : userSelfFollows.userdata){
Response <ResponseBody> response ;
Call <ResponseBody> call;
try {
call = ServiceFactory.createRetrofitService().getMediaOfUser(user.id,sessionMgr.getAuthToken());
response = call.execute();
}catch(IOException ex){
return Observable.error(ex);
}
if (response.isSuccessful()) {
try {
String str = responseHelper.streamToString( response.body().byteStream());
Gson gson = new GsonBuilder().create();
Media media = gson.fromJson(str, Media.class);
arAllMedia.addAll(media.mediaData);
} catch (IOException e) {
return Observable.error(e);
}
} else {
return Observable.error( new Exception( responseHelper.getErrorString( response.errorBody().byteStream())) );
}
}
return Observable.just(arAllMedia);
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ArrayList<Media.MediaData>>() {
@Override
public final void onCompleted() {
}
@Override
public final void onError(Throwable e) {
}
@Override
public final void onNext(ArrayList<Media.MediaData> arMedia) {
}
})
这是我到目前为止所做的,但它不会编译:
ServiceFactory.createRetrofitService().getUserFollowing(sessionMgr.getAuthToken())
.flatMapIterable(new Func1<UserSelfFollows, Iterable<?>>() {
@Override
public Iterable<?> call(UserSelfFollows userSelfFollows) {
return userSelfFollows.userdata;
}
})
.<Media.MediaData>flatMap(new Func1<UserSelfFollows.UserDataFollows, Observable<Media.MediaData>>() {
@Override
public Observable<Media.MediaData> call(UserSelfFollows.UserDataFollows user) {
return ServiceFactory.createRetrofitService().getMediaOfUser(user.id, sessionMgr.getAuthToken());
}
})
.toList();
错误是:
答案 0 :(得分:2)
如果我理解您的方案,您可以使用flatMapIterable
,然后使用flatMap
运营商,最后使用toList
收集所有改装调用结果。
在第一个flatMapIterable
中,您将从第一次调用(UserDataFollows
)获得的getUserFollowing()
列表展示为从此列表中发出多个项目的Obsevrable,然后{{1}创建一个flatMap
,对每个Observable
数据对象进行改造调用(它将并行发生,这似乎更适合此处,但如果您对顺序感兴趣,也可以使用UserSelfFollows.UserDataFollows
执行),然后一起收集所有最终数据,作为concatMap
对象的列表,您可以使用MediaData
toList
答案 1 :(得分:1)
我从未同时使用flatMapIterable
或concatMap
,但作为替代解决方案,您可以使用flatMap
和compose
运算符:
ServiceFactory.createRetrofitService().getUserFollowing(sessionMgr.getAuthToken())
.flatMap(list -> Observable.from(list))
.compose(getComposer())
.subscribe(mediaData -> mMediaDataList.add(mediaData), throwable -> {}, () -> { // on complete do something with mMediaList});
getComposer()
返回变形金刚UserSelfFollows
的位置 - &gt; Media.MediaData
protected Observable.Transformer<UserSelfFollows, Media.MediaData> getComposer() {
return ;
}
答案 2 :(得分:0)
Yosriz的回答让我朝着正确的方向前进。因此,为了避免其他人用这个来解决问题,这里是完整的代码解决方案:
ServiceFactory.createRetrofitService().getUserFollowing(sessionMgr.getAuthToken())
.flatMapIterable(new Func1<UserSelfFollows, Iterable<UserSelfFollows.UserDataFollows>>() {
@Override
public Iterable< UserSelfFollows.UserDataFollows > call(UserSelfFollows userSelfFollows) {
return userSelfFollows.userdata;
}
})
.flatMap(new Func1<UserSelfFollows.UserDataFollows, Observable<Media>>() {
@Override
public Observable<Media> call(UserSelfFollows.UserDataFollows user) {
return ServiceFactory.createRetrofitService().getMediaOfUser(user.id, sessionMgr.getAuthToken());
}
})
.toList()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<Media>>() {
@Override
public final void onCompleted() {
}
@Override
public final void onError(Throwable e) {
userMessageHandler.showDialog(mParentActivity, mParentActivity.getString(R.string.error_retrieving_data_title),
mParentActivity.getString(R.string.error_self_following_media) + e.getMessage(), 0);
}
@Override
public final void onNext(List<Media> arMedia) {
if (arMedia.size() == 0)
userMessageHandler.showToast(mParentActivity, mParentActivity.getString(R.string.warn_no_following));
else {
ArrayList<Media.MediaData> allMedia = new ArrayList<>();
for(Media media : arMedia){
allMedia.addAll(media.mediaData);
}
mBinding.gridview.setAdapter(new MediaGridViewAdapter(mParentActivity,FollowingViewModel.this, allMedia));
}
}
});