NSURLSessionConfiguration default constructor has surprising behavior

Originator:kristopherdjohnson
Number:rdar://24955482 Date Originated:03-Mar-2016 12:56 PM
Status:Open Resolved:
Product:OS X SDK Product Version:Xcode 7.2.1
Classification:UI/Usability Reproducible:Always
 
Summary:
Often when I write code using the NSURLSession API, I initially write this:

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

When I write this, my expectation is that config will be a default session configuration. But this code crashes at runtime with an "unrecognized selector" error.

The correct code is

    let config = NSURLSessionConfiguration.defaultSessionConfiguration()

I find it surprising that calling the default (and only) constructor for NSURLSessionConfiguration results in an unusable instance. Some web searches indicate that other people have been similarly surprised.

I propose that the NSURLSessionConfiguration no-arg constructor be changed so that it produces the same result as defaultSessionConfiguration(). If that would have a bad effect on existing applications, then I propose that the no-arg constructor be deprecated (with a warning from Xcode when used) in favor of a new constructor that makes the intent explicit, like

    NSURLSessionConfiguration(usingDefaultConfiguration: bool)

Steps to Reproduce:
Paste the following into an OS X Swift Playground:

import Cocoa
import XCPlayground

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

// This should be "let config = NSURLSessionConfiguration.defaultSessionConfiguration()"
let config = NSURLSessionConfiguration()

let url = NSURL(string: "http://www.apple.com")!
let session = NSURLSession(configuration: config, delegate: nil, delegateQueue: nil)
let dataTask = session.dataTaskWithURL(url) { (data, response, error) in
    if let error = error {
        print("error: \(error.localizedDescription)")
    }
    else {
        print("Received response")
    }
}
dataTask.resume()


Expected Results:
Should see "Received response" printed.

Actual Results:
Playground displays "Execution was interrupted, reason signal SIGABRT", with this in the debug output:

2016-03-03 12:49:34.919 URLSessionPlayground[17149:566179] -[NSURLSessionConfiguration disposition]: unrecognized selector sent to instance 0x7f8900426750


This can be fixed by changing "NSURLSessionConfiguration()" to "NSURLSessionConfiguration.defaultSessionConfiguration()"

Version:
Xcode 7.2.1 (7C1002) and OS X 10.11.3 (15D21)

Notes:
This issue is described for the OS X SDK, where I have tested it, but I believe this also applies to the iOS and tvOS SDKs.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!