更新:我正在使用 RxJava 1.x
这是以下代码段:
private static void tryObservableToMap() {
bad();
good();
}
private static void good() {
System.out.println("GOOD CASE");
String goodOutput =
m(m(m(m(m(Observable.from(ImmutableList.of("a","b","c","d")), "list")
.distinct(), "distinct")
.flatMap(s ->
m(m(Observable.fromCallable(() -> getIntForString(s)).subscribeOn(Schedulers.io()), "getInt " + s)
.map(intValue -> Pair.of(s, intValue)), "pair " + s)), "flatMap")
.toMap(Pair::getKey, Pair::getValue), "toMap")
.map(map -> map.entrySet().stream().map(e -> e.getKey() + ": " + e.getValue()).collect(Collectors.joining("\n"))), "OUTER")
.toBlocking()
.first();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("\nOutput:");
System.out.println(goodOutput);
}
private static void bad() {
System.out.println("BAD CASE");
String badOutput =
m(m(m(m(Observable.from(ImmutableList.of("a","b","c","d")), "list")
.distinct(), "distinct")
.flatMap(s ->
m(m(m(Observable.fromCallable(() -> getIntForString(s)).subscribeOn(Schedulers.io()), "getInt " + s)
.map(intValue -> Pair.of(s, intValue)), "pair " + s)
.toMap(Pair::getKey, Pair::getValue), "toMap " + s)), "flatMap")
.map(map -> map.entrySet().stream().map(e -> e.getKey() + ": " + e.getValue()).collect(Collectors.joining("\n"))), "OUTER")
.toBlocking()
.first();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("\nOutput:");
System.out.println(badOutput);
}
好与坏的区别在于,对于坏版本,我是.toMap在内部调用.flatMap而不是在.flatMap.
如果您运行此代码,您将看到作为执行一部分的所有 observable 的不同事件。
我想知道为什么“外部”可观察对象永远不会因坏情况而终止。对RX有更深了解的人可以解释一下吗?
慕田峪7331174
相关分类