gpt4 book ai didi

kotlin - 当时无法订阅

转载 作者:行者123 更新时间:2023-12-02 13:37:59 25 4
gpt4 key购买 nike

当我使用Mono.thenMany时,磁通数据丢失了,为什么?

@Test
fun thenManyLostFluxDataTest() {
Mono.empty<Int>()
.thenMany<Int> { Flux.fromIterable(listOf(1, 2)) }
.subscribe { println(it) } // why not output item 1, 2
}

如果更改为使用 blockLast()进行订阅,则测试方法将永远运行。太可怕了:
@Test
fun thenManyRunForeverTest() {
Mono.empty<Int>()
.thenMany<Int> { Flux.fromIterable(listOf(1, 2)) }
.blockLast() // why run forever
}

现在,我使用另一种方法来执行 thenMany方法应该执行的操作:
// this method output item 1, 2
@Test
fun flatMapIterableTest() {
Mono.empty<Int>()
.then(Mono.just(listOf(1, 2)))
.flatMapIterable { it.asIterable() }
.subscribe { println(it) } // output item 1, 2 correctly
}ed

最佳答案

您正在使用Kotlin的“lambda作为最后一个参数”的短格式语法。问题是,如果您查看thenMany方法签名,则它不接受Function,但接受Publisher

那么为什么lambda被接受,它代表什么呢?

实际上,它似乎被解释为Publisher(因为它只有1种方法subscribe(Subscriber))!

{ }替换( ),一切将恢复正常。

关于kotlin - 当时无法订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51837156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com