I created a new project today from the "Single View Application" template. I replaced ViewController.swift with the following:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "REDACTED")!
let request1 = URLRequest(url: url)
let request2 = URLRequest(url: url)
session.downloadTask(with: request1).resume()
session.downloadTask(with: request2).resume()
}
}
var session: URLSession = {
let configuration = URLSessionConfiguration.background(withIdentifier: "DownloadTest")
configuration.httpMaximumConnectionsPerHost = 1
return URLSession(
configuration: configuration,
delegate: SessionWorker(),
delegateQueue: nil
)
}()
class SessionWorker: NSObject, URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
print(downloadTask.taskIdentifier)
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let error = error {
print(error)
}
if let response = task.response as? HTTPURLResponse {
print(response.statusCode)
}
}
}
On iOS 9 (9.3.5, iPad Air 2 in my most recent test) devices, the didWriteData delegate method is called repeatedly for the first downloadTask, followed by a call to didCompleteWithError for the first downloadTask. Then, similar calls are made for the second downloadTask. On iOS 10 (10.2.1, iPhone 7 Plus in my most recent test), I receive calls many calls to didWriteData for both the first and second downloadTasks before a call to didCompleteWithError is made.
I have tested files from my company's servers, third party servers, and even the same URL for both downloadTasks. I have also tested on WIFI and LTE. All devices that download one file at a time are running some version of iOS 9. All devices that download many files at a time are running some version of iOS 10.
Was your test similar? Am I missing something?