Backward Concurrency in Xcode 13.2 RC

Xcode 13.2 was release with a new feature.

  • You can now use Swift Concurrency in applications that deploy to macOS Catalina 10.15, iOS 13, tvOS 13, and watchOS 6 or newer. This support includes async / await , actors, global actors, structured concurrency, and the task APIs. (70738378)

However, when in real practice, I found that Apple's APIs were still requesting higher OSes.

@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
extension URLSession {

    /// Convenience method to load data using an URLRequest, creates and resumes an URLSessionDataTask internally.
    ///
    /// - Parameter request: The URLRequest for which to load data.
    /// - Parameter delegate: Task-specific delegate.
    /// - Returns: Data and response.
    public func data(for request: URLRequest, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse)

    /// Convenience method to load data using an URL, creates and resumes an URLSessionDataTask internally.
    ///
    /// - Parameter url: The URL for which to load data.
    /// - Parameter delegate: Task-specific delegate.
    /// - Returns: Data and response.
    public func data(from url: URL, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse)

    /// Convenience method to upload data using an URLRequest, creates and resumes an URLSessionUploadTask internally.
    ///
    /// - Parameter request: The URLRequest for which to upload data.
    /// - Parameter fileURL: File to upload.
    /// - Parameter delegate: Task-specific delegate.
    /// - Returns: Data and response.
    public func upload(for request: URLRequest, fromFile fileURL: URL, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse)

    /// Convenience method to upload data using an URLRequest, creates and resumes an URLSessionUploadTask internally.
    ///
    /// - Parameter request: The URLRequest for which to upload data.
    /// - Parameter bodyData: Data to upload.
    /// - Parameter delegate: Task-specific delegate.
    /// - Returns: Data and response.

That means we still could not get the full benefit of async/await with old systems. Will Apple just leave APIs like that? Or we could use third-party APIs to replace?

Swift’s concurrency features will back deploy, Apple’s extensions in their own frameworks will not. If you want to use them on older systems you’ll need to provide your own implementations on older systems.

2 Likes

That was too bad. In that case, I may just leave old code unchanged and only apply async/await to new systems. I was about to refactoring my old code to async/await from AwaitKit.

Given how easy it is to wrap existing systems using the concurrency features I don’t know why would you couldn’t replace things anyway.

@Zhao_Xin this should help: Making async system APIs backward compatible | Swift by Sundell

3 Likes