1

Previously there were two types of request using iOS: sendSynchronousRequest and sendAsynchronousRequest. However, both are deprecated in iOS9. The recommended replacement for both cases: 1 and 2 is pretty the same:

[[NSURLSession sharedSession] dataTaskWithRequest:request
                                completionHandler:^(NSData *data,
                                        NSURLResponse *response,
                                        NSError *error) {
    //A piece of code after response completes.
}] resume];

So, is there no difference anymore between these two in the recent iOS? Why such a decision was made?

Community
  • 1
  • 1
Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53

1 Answers1

1

As a rule, as Apple reviews/upgrades APIs, they retire synchronous methods that could potentially block a thread and only introduce asynchronous renditions. And, where possible, these new asynchronous methods are generally cancelable, too. These two NSURLConnection convenience methods deserved to be retired.

While I don't recall Apple explicitly articulating this strategy, one can logically infer this from the evolution seen in many of their frameworks (NSURLSession, the Contacts framework, the Photos framework, etc.). I'm sure they're doing this because synchronous APIs are so susceptible to misuse, almost inviting poor programming patterns (notably, the blocking of the main thread).

Rob
  • 415,655
  • 72
  • 787
  • 1,044