Move from Combine to Swift concurrency?

You're probably using (or planning to use) withCheckedThrowingContinuation to bridge the Network.framework API to the async/await world, right?

The general pattern to support cancellation is to wrap the withCheckedThrowingContinuation call in a call to withTaskCancellationHandler. This calls you back when the surrounding task gets cancelled, giving you a chance to forward the cancellation to your network connection. I don't know much about Network.framework, but you cancelling the NWConnection will then hopefully trigger a callback from Network.framework into your code, which you can use to resume the continuation with a CancellationError().

Note that the onCancel: handler of withTaskCancellationHandler runs in a different concurrency context than the task in which you set up the network connection, so you may need to introduce a Mutex or similar to synchronize access to your state.

See also this thread for inspiration: Automatically Cancelling Continuations. (Although IIRC the semantics of @nikolai.ruhe's withCancellingContinuation API are to resume the continuation directly without going through the library you're interfacing with first – Network.framework in your case. Not sure if this is what you want.)

1 Like