Issue
Modern concurrency with the new Async / Await
was introduced for iOS 15 and above with Swift 5.5 but very soon, with the release of Xcode 13.2 (and subsequently 13.2.1) it enabled us to use Async
and Await
to develop for iOS 13+, macOS 10.15+ etc. However, when I try to make an asynchronous request like this:
let (data, response) = try await URLSession.shared.data(for: request)
It does not run on iOS 13+. Instead, I get an error stating:
data(for:delegate:)
is only available in iOS 15.0 or newer
The error goes away when I set minimum deployment target to iOS 15.0, but I want the software to support iOS 13.0+. I understand that data(for:delegate:)
is supported only on iOS 15.0+, but what is the point of backward compatibility to 13.0+, if I am not able to make an asynchronous network fetch request?
data(for:delegate:) documentation -> which states minimum iOS required is 15.0
Xcode release notes -> where they mentioned a clang bug-fix to deploying Swift concurrency features to iOS prior to 15
Solution
As stated in this Swift by Sundell article:
Although Swift 5.5’s new concurrency system is becoming backward compatible in Xcode 13.2, some of the built-in system APIs that make use of these new concurrency features are still only available on iOS 15, macOS Monterey, and the rest of Apple’s 2021 operating systems.
This is how John replicates an async/await-powered URLSession API for one method:
@available(iOS, deprecated: 15.0, message: "Use the built-in API instead")
extension URLSession {
func data(from url: URL) async throws -> (Data, URLResponse) {
try await withCheckedThrowingContinuation { continuation in
let task = self.dataTask(with: url) { data, response, error in
guard let data = data, let response = response else {
let error = error ?? URLError(.badServerResponse)
return continuation.resume(throwing: error)
}
continuation.resume(returning: (data, response))
}
task.resume()
}
}
}
Answered By - Gustavo Conde Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.