Major NSURLSession performance issues with iOS 10 / Xcode 8 / Swift 2.3

I'm experiencing major performance issues with NSURLSession and NSURLSessionDownloadTasks in iOS 10. Downloads are happening at maybe 1/30th of the speed (downloads that took 60 seconds under iOS 9 are taking 20-30 minutes) as normal, and about half the time the download will run for a while and then fail with a cryptic message indicating "unsupported url" with error code -1002, but the 'userInfo' field of the NSError object isn't further populated. Very strange as the download had already been running for a while and retrieved data, and attempting the same download with the same URL again might finish and the downloaded data is fine.


Download Failed. Error: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL}


These performance/stability issues are happening both in my development environment, as well as with the previous production version of the app downloaded from the App Store on my iOS 10 device that functioned just fine on iOS 9 and iOS 8.


I just updated my device (iPhone 6) to iOS 10, and subsequently my development environment to Xcode 8, and revised my app for compliance with Swift 2.3. In testing, I'm seeing that the 'URLSession didWriteData' delegate method is only getting called a few times per second, with very low 'byes received' counts.


Sample output from logging call in didWriteData delegate showing only a few calls per second:

2016-09-15 12:09:55.178:  Starting download.
2016-09-15 12:09:55.947:  URLSession didWriteData: received 16384 bytes, total 16384, total expected 237143733, 6.90889e-05% Complete
2016-09-15 12:09:56.036:  URLSession didWriteData: received 499 bytes, total 16883, total expected 237143733, 7.11931e-05% Complete
2016-09-15 12:09:56.143:  URLSession didWriteData: received 17408 bytes, total 34291, total expected 237143733, 0.0001446% Complete
2016-09-15 12:09:56.250:  URLSession didWriteData: received 17408 bytes, total 51699, total expected 237143733, 0.000218007% Complete
2016-09-15 12:09:56.360:  URLSession didWriteData: received 33792 bytes, total 85491, total expected 237143733, 0.000360503% Complete
2016-09-15 12:09:56.469:  URLSession didWriteData: received 18432 bytes, total 103923, total expected 237143733, 0.000438228% Complete
2016-09-15 12:09:56.847:  URLSession didWriteData: received 16384 bytes, total 120307, total expected 237143733, 0.000507317% Complete
2016-09-15 12:09:56.957:  URLSession didWriteData: received 17408 bytes, total 137715, total expected 237143733, 0.000580724% Complete
2016-09-15 12:09:57.070:  URLSession didWriteData: received 18432 bytes, total 156147, total expected 237143733, 0.000658449% Complete
2016-09-15 12:09:57.175:  URLSession didWriteData: received 17408 bytes, total 173555, total expected 237143733, 0.000731856% Complete
2016-09-15 12:09:57.357:  URLSession didWriteData: received 16384 bytes, total 189939, total expected 237143733, 0.000800945% Complete
2016-09-15 12:09:57.467:  URLSession didWriteData: received 17408 bytes, total 207347, total expected 237143733, 0.000874352% Complete



The NSURLSession is configured like so:

let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(BACKGROUND_SESSION_ID)
config.HTTPMaximumConnectionsPerHost = 1
config.sessionSendsLaunchEvents = true
config.discretionary = false
config.allowsCellularAccess = true

session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)


And I'm starting the download task like this:


downloadTask = self.session?.downloadTaskWithURL(urlObject!)

if(downloadTask != nil) {
    downloadTask!.resume()
}



Am I doing something wrong configuring these downloads? Has anyone experienced this issue?


Thanks in advance!

Replies

Can you reproduce this in a small test app?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I've a similar setup and I have not had any noticable issue with the performance of downloads. I do however experience the Code=-1002 "unsupported URL" issue when the network is turned off, resulting in resuming the task from the start (losing the downloaded data) when network becomes available. I also experienced cancelByProducingResumeData whilst it provides the data for resuming, it is not accepted when calling resume on the task.


I did find this explanation and workaround for this last issue: http://stackoverflow.com/a/39347461/1012802


I've logged bug reports: 28447649 and 28003939 respectively. I reported 28003939 on the beta6 back in august and it has been marked a duplicate of 27744391, ive also read this bug was as early as beta1.

Looks like this issue was noticed as a bug by Apple in iOS 10 and is fixed in the upcoming 10.2 beta.

https://forums.developer.apple.com/message/182311#182311