Logo for tanaschita.com

Using URLSession with async/await in Swift

Learn how to make HTTP networking requests with URLSession's async/await API.

17 Oct 2022 · 3 min read

Along with the release of async/await in Swift 5.5, Apple has also extended many of their asynchronous APIs to support the async/await concurrency model.

In this article, we'll look at how to use async/await methods for URLSession to send requests or to download and upload data to a server.

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

Sending a URL request

When working with URLSession, we can use the URLSessionDataTask type to send requests and receive responses.

Prior to Swift's async/await feature, completion handlers or delegates were the standard pattern to use. Sending a request by using a completion handler looks as follows:

let task = URLSession.shared.dataTask(with: urlRequest) { data, response, error in
// Handle response.
}
task.resume()

With async/await, the same can be done in one line:

let (data, response) = try await URLSession.shared.data(for: urlRequest)

Downloading data

A download request works almost the same as the request we looked at above with the only difference that we get a file url instead of a data object as a response.

With a completion handler, a download request looks as follows:

let task = URLSession.shared.downloadTask(with: url) { fileURL, response, error in
// Handle response
}
task.resume()

When using async/await, the same request can also be reduced to one line:

let (fileURL, response) = try await URLSession.shared.download(from: url)

Uploading data

When uploading data to a server, we can either pass in a Data object or a file url.

With a completion handler, an upload request looks as follows:

let task = urlSession.uploadTask(with: urlRequest, from: data) { data, response, error in
// Handle response
}
task.resume()

When using async/await, the same request can also be reduced to one line:

let (data, response) = try await URLSession.shared.upload(for: urlRequest)
Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

async/await

concurrency

swift

How to bridge completions handlers to Swift's async/await

Understand Swift continuations to create your own async/await functions.

05 Dec 2022 · 3 min read

Latest articles and tips

© 2023 tanaschita.com

Privacy policy

Impressum