Hi, did anyone encounter the problem of publisher ...
# multiplatform
a
Hi, did anyone encounter the problem of publisher being immediately canceled using KMPNativeCoroutinesCombine? I’ve tried a simple test with Integer flow, like so:
Copy code
@NativeCoroutinesState
val test: StateFlow<Int> = _test.asStateFlow()
In Swift, in the
init()
block of my view, I’m calling following function:
Copy code
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 Clephas
r
The
sink
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 statements
👍 1
a
Thank you very much 🙂