Greetings,
Apple: Thanks for all the hard work you have been doing to make iOS easier for developers. A lot of great things are happening. Keep up the good work!
I noticed that in iOS 9 (Xcode 7.0 beta 7A121l), Apple is depreciating NSURLConnection.sendSynchronousRequest in both Swift, and Objective-C. Here is the warning I am seeing:
Swift warning:
'sendSynchronousRequest:_:returningResponse:)' was deprecated in iOS 9.0 - Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h
Objective-C warning:
'sendSynchronousRequest:returningResponse:error:' is deprecated: first deprecated in iOS 9.0 - Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h
Please let me know if I am missing something, but I think it is a huge mistake for Apple to depricate the NSURLConnection.sendSynchronousRequest method.
Your reasons for depricating may not handle all the "real world" use cases. I am imagining that your justification for depricating NSURLConnection.sendSynchronousRequest is that you want us to use NSURLSession because it is asynchronous, and you want to discourage synchronous network requests. I am very familiar with NSURLSession, and have used it a lot to do asynchronous network requests. However, there is a definite, "real world" use case it does not handle. Here is some pseudo code that describes the use case where we need NSURLConnection.sendSynchronousRequest:
dispatch async or whatever way we want to get off the main ui thread {
Make network calls synchronously to fetch multiple accounts (outer loop) {
Make network calls synchronously to fetch products in each account (inner loop) {
}
}
reload UI on main UI thread
}
In the above example, I need to make multiple synchronous calls, serially. I can do that off the main UI thread. However, they must be done synchronously because the network calls in the inner loop rely on the output from the first call in the outer loop.
For the above mentioned used case, here are my available UNACCEPTABLE solutions (that I know of).
1) Use a dispatch_semaphore with NSURLSession. I think this is a very bad option. In my opinion this is unacceptable because it's going to create a lot of extra code on my end (the customer) and be difficult to maintain, and more prone to bugs. I think that the dispatch_semaphore with NSURLSession is very clunky and unnecessary since we already have NSURLConnection.sendSynchronousRequest: which works great, is very little code, and very easy for me (the customer) to maintain, and use. This is a very bad for me, possible solution, that's going to be extremely difficult to implement and maintain.
2) NSData(contentsOfURL. This is not sophisticated enough because it does not allow me to build up a NSMutableURLRequest. This does not work.
3) Use NSURLSession. This won't work because it is asynchronous. I need to fetch the accounts first, then iterate through each account to fetch products, and it needs to be done synchronously. The wrapper around the whole thing can be done off the main UI thread, but what is inside needs to be done synchronously. This does not work.
Therefore, I would strongly urge that Apple not depricate NSURLConnection.sendSynchronousRequest because it breaks code that already works, and does not handle the "real world" use case I mentioned above, which would run off of the main UI thread.
Thanks in advance for looking at this.