Ana Sekuloski
03/25/2024, 12:06 PM@NativeCoroutinesState
val test: StateFlow<Int> = _test.asStateFlow()
In Swift, in the init()
block of my view, I’m calling following function:
private func subscribeToPublisher() {
let testPublisher = createPublisher(for: viewModel.testFlow)
testPublisher
.handleEvents(receiveSubscription: { _ in print("Subscribed") },
receiveCancel: { print("Cancelled") })
.sink(receiveCompletion: { completion in switch completion {
case .finished:
print("Completed with success")
break
case let .failure(throwable):
print("Completed with failure: \(throwable)")
break
}}, receiveValue: {
value in print(value) }
)
}
but it looks like that I don’t receive updated value (“Cancelled” Is printed right after “Subscribed”). What might be that I’m doing wrong? @Rick ClephasRick Clephas
03/25/2024, 12:11 PMsink
call returns an AnyCancellable
that allows you to cancel the subscription.
Once an AnyCancellable
is deallocated it will also cancel the subscription.
Since the cancellable isn’t stored anywhere it gets deallocated immediately, which likely means you won’t see any of the print statementsAna Sekuloski
03/25/2024, 12:28 PM