Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 当flatMap中的一个完成时,使原始可观察完整_Kotlin_Rx Java2 - Fatal编程技术网

Kotlin 当flatMap中的一个完成时,使原始可观察完整

Kotlin 当flatMap中的一个完成时,使原始可观察完整,kotlin,rx-java2,Kotlin,Rx Java2,我有一个可观察的,我正在其上应用flatMap操作符。当第二个完成时,是否有可能使该原始可见完成 这是代码 Observable.never<Int>() .startWith(0) .doOnComplete { println("Completed") } // Not called. .flatMap { Observable.fromArray(1, 2, 3, 4, 5) /* Completes after 5 */ }

我有一个
可观察的
,我正在其上应用
flatMap
操作符。当第二个
完成时,是否有可能使该原始
可见
完成

这是代码

Observable.never<Int>()
        .startWith(0)
        .doOnComplete { println("Completed") } // Not called.
        .flatMap { Observable.fromArray(1, 2, 3, 4, 5) /* Completes after 5 */ }
        .subscribe(::println)
我正在尝试实现以下输出:

1
2
3
4
5
Completed
1
2
3
4
5
Completed

您可以使用操作符
materialize
,这将帮助您获得有关
flatMap
内部形状的信息。然后,当您收到
onComplete
通知时,您可以在上游进行处置(只接受
onNext
通知)

    Observable.<Integer>never()
            .startWith(0)
            .flatMap(integer -> Observable.range(1, 5)
                    .materialize())
            .takeWhile(notification -> notification.isOnNext())
            .map(notification -> notification.getValue())
            .doOnComplete(() -> System.out.println("Completed"))
            .subscribe(integer -> System.out.println(integer));
1
2
3
4
5
Completed