URLSessionConfiguration.background(withIdentifier:) implementation

Hi Guys,

I am trying to understand how tasks added into Sessions with the background configuration, in order to implement URLSessionConfiguration.background(withIdentifier:) method.

As per Apple's documentation,

A session configured with this object hands control of the transfers over to the system, which handles the transfers in a separate process. In iOS, this configuration makes it possible for transfers to continue even when the app itself is suspended or terminated.

If the system is taking control over the transfer, then a new session with the same background-identifier should be able to fetch any existing background tasks. I ran this small piece of code to confirm and it did work as I was expecting

let urlString = "https://developer.apple.com/assets/elements/icons/brandmark/apple-developer-brandmark.svg"
let identifier = "com.swiftfoundation.test_urlSessionConfigurationBackground"
let session = URLSession(configuration: URLSessionConfiguration.background(withIdentifier: identifier))
let task = session.dataTask(with: URL(string: urlString)!)
task.resume()
let taskIdentifier = task.taskIdentifier

DispatchQueue.global().async {
    let session2 = URLSession(configuration: URLSessionConfiguration.background(withIdentifier: identifier))
    session2.getAllTasks { (tasks) in
        let filter = tasks.filter { $0.taskIdentifier == taskIdentifier }
        if filter.first?.currentRequest?.url?.absoluteString == urlString {
            print("Found")
        } else {
            print("Not Found")
        }
    }
}

The above code prints "Found" all the time.

Here are my questions,

  1. Is this the same expected behavior for apple/swift-corelibs-foundation project?
  2. If so, as I don't know much about creating a system process, can I get some guidance from this good dev-community to implement.
    a. How to transfer a URLSession or easy_handle or multi_handle over to system
    b. How to retrieve them back later.

This could be a good learning experience for me. Please and thank you in advance.

Terms of Service

Privacy Policy

Cookie Policy