我正在尝试使用Observable.zip对api进行并行调用。这些api调用中的每一个都返回一个Observable>。我正在创建一个Observable列表,并将该列表与返回由这些api调用返回的单个Observable>组成的Observable>函数一起发送至zip。出于某些原因,object []可以用20个TreeSet项初始化,这很好,但是每个TreeSet为空,尽管在日志中我可以发现api调用包含数据的响应。
subscription = repository
.getFollowers(id, OFFSET)
.subscribeOn(Schedulers.io())
.map(FollowerResponse::getCollection)
.flatMap((Function<List<Follower>, Observable<TreeSet<Track>>>) followers -> {
List<Observable<TreeSet<Track>>> list = new ArrayList<>();
observable = repository.getTracks(OFFSET, String.valueOf(followers.get(0).getId()));
list.add(observable.subscribeOn(Schedulers.io())
.onErrorResumeNext(Observable.empty()));
for (int i = 1; i < followers.size(); i++) {
observable = Observable.merge(observable, repository.getTracks(OFFSET, String.valueOf(followers.get(i).getId())));
list.add(observable.subscribeOn(Schedulers.io())
.onErrorResumeNext(Observable.empty()));
}
return Observable.zip(list, new Function<Object[], TreeSet<Track>>() {
@Override
public TreeSet<Track> apply(Object[] objects) throws Exception {
TreeSet<Track> treeSet = new TreeSet<>();
Log.e("zip ", "inside zip");
for (Object obj : objects) {
treeSet.addAll((TreeSet<Track>) obj);
}
return treeSet;
}
});
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<TreeSet<Track>>() {
@Override
public void accept(TreeSet<Track> tracks) throws Exception {
Log.e("zip ", "inside subscribe");
}
});
答案 0 :(得分:0)
该死,我只是意识到我没有删除在Observable.zip之前尝试使用的Observable.merge!确实有帮助发布,就像在想一声大声笑