Last active
October 23, 2025 19:13
-
-
Save dterekhov/80c62ea5a089c531098d4d683c73ffee to your computer and use it in GitHub Desktop.
Swift, Concurrency: Universal escape hatch from Swift structured concurrency #swift-concurrency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Universal escape hatch. | |
| /// | |
| /// [Original source](https://stackoverflow.com/a/78894560/4370543) | |
| struct UncheckedSendable<T>: @unchecked Sendable { | |
| let unwrap: T | |
| init(_ value: T) { unwrap = value} | |
| } | |
| // A sample of usage with `SendBirdCalls` iOS SDK. Where it was impossible to have async version (was a crash). | |
| // MARK: - Process with CXProvider | |
| extension CXCallManager { | |
| /// Specific escape hatch. | |
| struct UncheckedSendableCompletion: @unchecked Sendable { | |
| let unwrap: (Error?) -> Void | |
| init(_ value: @escaping (Error?) -> Void) { unwrap = value } | |
| } | |
| /// (!) Completion version instead of async. | |
| func reportIncomingCall(with callID: UUID, update: CXCallUpdate, completion: UncheckedSendableCompletion) { | |
| self.provider.reportNewIncomingCall(with: callID, update: update) { (error) in | |
| completion.unwrap(error) | |
| } | |
| } | |
| func reportIncomingCall(with callID: UUID, update: CXCallUpdate) async throws { | |
| try await self.provider.reportNewIncomingCall(with: callID, update: update) | |
| } | |
| func endCall(for callId: UUID, endedAt: Date, reason: DirectCallEndResult) { | |
| guard let endReason = reason.asCXCallEndedReason else { return } | |
| self.provider.reportCall(with: callId, endedAt: endedAt, reason: endReason) | |
| } | |
| func connectedCall(_ call: DirectCall) { | |
| let connectedAt = Date(timeIntervalSince1970: Double(call.startedAt) / 1000) | |
| self.provider.reportOutgoingCall(with: call.callUUID!, connectedAt: connectedAt) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment