I noticed that 'sink' never was called when using 'receive' . I tested on Xcode 11 beta 5.
public enum SomeError: Error {
case someError
}
Just("1")
.tryMap { val -> String in
if Bool.random() { throw SomeError.someError }
return val
}
.flatMap { val -> Empty<String, Error> in
return .init()
}
.receive(on: RunLoop.main)
.sink(receiveCompletion: { completion in
print(1, completion)
switch completion {
case .failure(let error):
print(3, error)
case .finished:
break
}
}) { val in
print(2, val)
}
The sink modifier returns an AnyCancellable. When the AnyCancellable is destroyed, it cancels the subscription. In your example, you're not storing the AnyCancellable, so it is destroyed immediately, cancelling the subscription immediately.
I've changed my code like this let requestCancellable = Just("1") but 'sink' isn't invoked.
When I remove .receive(on: RunLoop.main) then 'sink' start to be invoked.
If I remove 'flatMap' or 'receive' then 'sink' is started to invoke. I can't understand how it relative to AnyCancelable. I've written a simple code to test it.
let requestCancellable = Just("1")
.flatMap { val -> Empty<String, Never> in
return .init()
}
.receive(on: RunLoop.main)
.sink(receiveCompletion: { completion in
print(1, completion)
}) { val in
print(2, val)
}
It's entirely possible that by shortening the pipeline, you're winning the race between the deinitialization of your AnyCancellable and sink being called. Have you ensured its lifetime is properly managed?