为什么在某些情况下Observable.fromIterable会丢失一些数据?

时间:2018-11-16 09:00:18

标签: rx-java2

我在RxJava2中编写了一个测试flatMap运算符的示例,下面是代码片段:

Observable<Integer> observable = ObservableCreate.create(new ObservableOnSubscribe<Integer>() {
        @Override
        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
            emitter.onNext(1);
            emitter.onNext(2);
            emitter.onNext(3);
            emitter.onNext(4);
            emitter.onComplete();
        }
    });
    Disposable disposable = observable.flatMap(new Function<Integer, ObservableSource<String>>() {
        @Override
        public ObservableSource<String> apply(Integer i) throws Exception {
            List<String> list = new ArrayList<>(3);
            for(int j = 0; j < 3; j++) {
                list.add("I am from apply:" + i);
                //list.add("I am from apply:" + i + "_" + j);
            }
            log("apply: " + listToString(list));
            return Observable.fromIterable(list);
        }
    })
    .flatMap(new Function<String, ObservableSource<String>>() {
        @Override
        public ObservableSource<String> apply(String s) throws Exception {
            return Observable.create(new ObservableOnSubscribe<String>() {
                @Override
                public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                    emitter.onNext("flatMap--" + s);
                    emitter.onComplete();
                }
            });
        }
    })
    .subscribe(
        s -> log("accept: " + s)
        , throwable -> log(throwable.getMessage())
        , () -> log("complete!")
    );

您认为将打印哪些日志?会发生以下情况吗?

apply: [I am from apply:1, I am from apply:1, I am from apply:1]
accept: flatMap--I am from apply:1
accept: flatMap--I am from apply:1
accept: flatMap--I am from apply:1
apply: [I am from apply:2, I am from apply:2, I am from apply:2]
accept: flatMap--I am from apply:2
accept: flatMap--I am from apply:2
accept: flatMap--I am from apply:2
apply: [I am from apply:3, I am from apply:3, I am from apply:3]
accept: flatMap--I am from apply:3
accept: flatMap--I am from apply:3
accept: flatMap--I am from apply:3
apply: [I am from apply:4, I am from apply:4, I am from apply:4]
accept: flatMap--I am from apply:4
accept: flatMap--I am from apply:4
accept: flatMap--I am from apply:4
complete!

我也认为我会得到这些日志,但是我错了,我每个字符串只能得到两个,就像这样:

apply: [I am from apply:1, I am from apply:1, I am from apply:1]
accept: flatMap--I am from apply:1
accept: flatMap--I am from apply:1
apply: [I am from apply:2, I am from apply:2, I am from apply:2]
accept: flatMap--I am from apply:2
accept: flatMap--I am from apply:2
apply: [I am from apply:3, I am from apply:3, I am from apply:3]
accept: flatMap--I am from apply:3
accept: flatMap--I am from apply:3
apply: [I am from apply:4, I am from apply:4, I am from apply:4]
accept: flatMap--I am from apply:4
accept: flatMap--I am from apply:4
complete!

我试图找出原因,所以我添加了一些断点并逐步调试,不幸的是,调试后我得到了正确的结果。我很困惑,fromIterable运算符在某些情况下会丢失一些数据吗?

0 个答案:

没有答案