I'm experiencing some behaviour that I did not expect.
For example
let cancellable = [1, 2, 3]
.publisher
.flatMap { _ -> Empty<Void, Never> in
print("Entered")
return Empty(completeImmediately: true)
}.print("DEBUG").sink(receiveValue: { })
which prints
DEBUG: receive subscription: (FlatMap)
DEBUG: request unlimited
Entered
Entered
Entered
DEBUG: receive finished
but I would expect it to only print Entered
once. I would expect it to finish immediately after the flatMap
.
Similarly
let subject: PassthroughSubject<Void, Never> = PassthroughSubject()
let cancellable = subject
.flatMap { _ -> Empty<Void, Never> in
print("Entered")
return Empty(completeImmediately: true)
}.print("DEBUG").sink(receiveValue: { })
subject.send(())
prints
DEBUG: receive subscription: (FlatMap)
DEBUG: request unlimited
Entered
It never finishes!
I would expect it to finish.
The same behaviour also happens with map { ... }.switchToLatest()
which is even stranger in my opinion.
Can someone help me understand what's happening and why?