Browse Questions
- All three transform collections but handle the results differently.
- Secure Message is a stateless asymmetric primitive that encrypts or signs a single payload using your private key and the peer's public key, making it a natural fit for request/response APIs.
- A race condition occurs when two or more threads access shared mutable state simultaneously, producing unpredictable results depending on timing.
- withCheckedContinuation bridges old callback-based APIs into the async/await world.
Answer:
withCheckedContinuation bridges old callback-based APIs into the async/await world. Use it to wrap any completion-handler function so it can be awaited.
Code Example:
// Old callback-based API
func fetchData(completion: @escaping (Data?, Error?) -> Void) { ... }
// Wrap it for async/await use
func fetchData() async throws -> Data {
try await withCheckedThrowingContinuation { continuation in
fetchData { data, error in
if let error = error {
continuation.resume(throwing: error)
} else if let data = data {
continuation.resume(returning: data)
}
}
}
}
// Now you can await it
let data = try await fetchData()
Key Rules:
continuation.resume()must be called exactly once — never zero, never twice- Use
withCheckedThrowingContinuationwhen the operation can fail withUnsafeContinuationskips the "called once" check — only use if performance is critical