How do you handle asynchronous operations in Swift without using third-party libraries?
Instruction: Discuss the native approaches available in Swift for managing asynchronous tasks.
Updated
Official answer
Use Swift concurrency for asynchronous APIs that support it: async functions can suspend at await points, and throwing async calls preserve normal do/catch handling. async/await is a language concurrency model, not a promise that arbitrary synchronous work moves to a background thread. A Task can inherit actor isolation; heavy synchronous work on the main actor still blocks UI progress.
Use async let for a small known set of child operations and task groups for dynamic child work. Keep concurrency bounded, propagate errors and handle cancellation deliberately. Structured child tasks remain within their scope; Task-created unstructured work needs an owner and a cancellation policy. A cancelled task must cooperate by checking cancellation or using APIs that do so.
GCD is useful for serial coordination and legacy callback APIs. Choose a queue and quality of service that reflect the work, and return UI changes to the main-actor boundary. Never synchronously wait for work that requires the same serial queue. Dispatching an asynchronous request from a block does not mean that request finishes when the block returns.
OperationQueue provides cancellable operations, dependencies and a concurrency limit. A dependency waits for another operation to finish, which does not imply it succeeded. An operation that starts asynchronous work must accurately represent its executing/finished lifecycle; otherwise dependent work can begin too early. Cancellation also does not automatically interrupt arbitrary work.
When adapting a callback to a checked continuation, resume exactly once on all success/failure paths and define cancellation handling. Test cancellation before and after completion, partial failure, repeated callbacks and out-of-order results. Choose the simplest native abstraction that fits the lifecycle rather than layering all three into every feature.
Swift concurrency and isolation, OperationQueue, DispatchGroup contract