问题描述
当我使用Mono.thenMany
时,通量数据丢失了,为什么?
When I use Mono.thenMany
, the Flux data is lost, why?
@Test
fun thenManyLostFluxDataTest() {
Mono.empty<Int>()
.thenMany<Int> { Flux.fromIterable(listOf(1, 2)) }
.subscribe { println(it) } // why not output item 1, 2
}
如果我更改为使用blockLast()
进行订阅,则测试方法将永远运行.太可怕了:
If I change to use blockLast()
to do the subscribe, the test method run forever. So fearful:
@Test
fun thenManyRunForeverTest() {
Mono.empty<Int>()
.thenMany<Int> { Flux.fromIterable(listOf(1, 2)) }
.blockLast() // why run forever
}
现在,我使用另一种方法来执行thenMany
方法应做的事情:
Now I use another way to do what the thenMany
method should do:
// 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
.
You are using Kotlin's "lambda as a last parameter" short-form syntax. The thing is, if you look at the thenMany
method signature, it doesn't accept a Function
, but a Publisher
.
那么为什么lambda被接受,它代表什么呢?
So why is that lambda accepted, and what does it represent?
实际上它似乎被解释为Publisher
(因为它只有1种方法,subscribe(Subscriber)
)!
It seems to be in fact interpreted as a Publisher
(as it has only 1 method, subscribe(Subscriber)
)!
将{ }
替换为( )
,一切将恢复正常.
Replace the { }
with ( )
and everything will be back to normal.
这篇关于当时无法订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!