PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 30, 2022

[FIXED] Why do I see an error about 'data(for:delegate:) is only available on iOS 15.0+' even though modern concurrency is backward compatible

 September 30, 2022     concurrency, ios, swift, urlsession, xcode     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing