Structure_Concurrency5.9.0
UnsafeContinuation
A mechanism to interface between synchronous and asynchronous code, without correctness checking.
- iOS
- 13.0+
- macOS
- 10.15+
- tvOS
- 13.0+
- watchOS
- 6.0+
@frozen struct UnsafeContinuation<T, E> where E : Error
A continuation is an opaque representation of program state. To create a continuation in asynchronous code, call the withUnsafeContinuation(_:)
or withUnsafeThrowingContinuation(_:)
function. To resume the asynchronous task, call the resume(returning:)
, resume(throwing:)
, resume(with:)
, or resume()
method.
CheckedContinuation
performs runtime checks for missing or multiple resume operations. UnsafeContinuation
avoids enforcing these invariants at runtime because it aims to be a low-overhead mechanism for interfacing Swift tasks with event loops, delegate methods, callbacks, and other non-async
scheduling mechanisms. However, during development, the ability to verify that the invariants are being upheld in testing is important. Because both types have the same interface, you can replace one with the other in most circumstances, without making other changes.
Citizens in _Concurrency
Conformances
protocol Sendable
A type whose values can safely be passed across concurrency domains by copying.
Members
func resume(returning: T
) Resume the task that’s awaiting the continuation by returning the given value.
func resume(returning: T
) Resume the task that’s awaiting the continuation by returning the given value.
func resume(throwing: E
) Resume the task that’s awaiting the continuation by throwing the given error.
Citizens in _Concurrency
where E:Error
Members
func resume(
) Resume the task that’s awaiting the continuation by returning.
func resume(with: Result<T, E>
) Resume the task that’s awaiting the continuation by returning or throwing the given result value.
func resume<Er>(with: Result<T, Er>
) Resume the task that’s awaiting the continuation by returning or throwing the given result value.