RxJava 1.0:阻塞直到嵌套订阅者完成

时间:2018-04-01 10:31:00

标签: rx-java

我有以下流程。我想阻止,直到我完成嵌套订阅者中的所有Google调用。如果我将 toBlocking ,则代码不会运行。基本上,我想在完成对Google的所有调用后访问缓存。

const background = this.state.animationValue.interpolate({
  inputRange: [0, 1],
  outputRange: [0, 1] 
});

1 个答案:

答案 0 :(得分:0)

不要将其嵌套到单独的subscribe中,而是使用flatMap

cartesian
.doOnCompleted(() -> { System.out.println(" DOC#1 # ELEMENTS IN CACHE " + cache.size()); })
.filter((CartesianProduct cp) -> !cache.containsKey(cp.fromTo))
.groupBy((cp) -> cp.from)
.flatMap((group) -> {
    GeoLocation from = (GeoLocation) ((GroupedObservable)group).getKey();
    //Google Maps API allows max 25 Destinations per call
    return ((GroupedObservable)group).window(25)
                    .doOnCompleted(() -> { System.out.println(" DOC#2 # ELEMENTS IN CACHE " + cache.size()); });
})
.flatMap((window) -> {
    return ((rx.subjects.UnicastSubject)window).asObservable().toList()
                    .observeOn(Schedulers.from(httpExecutor))
                    .doOnCompleted(() -> { System.out.println(" DOC#3 # ELEMENTS IN CACHE " + cache.size()); });
})
.flatMap( (list) -> {
    List<CartesianProduct> toList = (List<CartesianProduct>) list;
    List<GeoLocation> targets = new ArrayList();
    for (CartesianProduct cP : toList) {
            targets.add(cP.to);
    }
    return GoogleMapsClientWrapper.getDistanceAndDurationForDirection(from, targets, cache);
})
.subscribe(s -> {
    System.out.println("Everything is done");
});