Why Do I See An Error About 'data(for:delegate:) Is Only Available On IOS 15.0+' Even Though Modern Concurrency Is Backward Compatible
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
Answer
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()
}
}
}
Related Questions
- → Function Undefined in Axios promise
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Click to navigate on mobile devices
- → Playing Video - Server is not correctly configured - 12939
- → How to allow api access to android or ios app only(laravel)?
- → Axios array map callback
- → Access the Camera and CameraRoll on Android using React Native?
- → Update React [Native] View on Day Change
- → Shopify iOS SDK - issue converting BuyProductVariant to BuyProduct
- → BigCommerce and shopify API
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → React Native - Differences between Android and IOS
- → What is the difference between React Native and React?