https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Ana Sekuloski

03/25/2024, 12:06 PM
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

Rick Clephas

03/25/2024, 12:11 PM
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

Ana Sekuloski

03/25/2024, 12:28 PM
Thank you very much 🙂
3 Views