Featured post

Difference between protocol and delegates?

A protocol, declared with the ( @protocol syntax in Objective-C) is used to declare a set of methods that a class "adopts" (de...

Friday, 10 April 2015

NSMutableURLRequest and NSURLConnection can't work in GCD dispatch_async?

It's not that you're using a mutable connection in one place and not the other, it's that you're calling the synchronous request method which runs immediately on the current thread versus an asynchronous method which needs a run loop to operate. From the documentation for -[NSURLConnection start]:
If you don’t schedule the connection in a run loop or an operation queue before calling this method, the connection is scheduled in the current run loop in the default mode.
Calling the asynchronous method from a block on a background thread is redundant. You should either call the async method on the main thread (it returns immediately and schedules its work in the background) or call the synchronous method in the async dispatched block. Doing it the second way, you don't have to deal with all the delegate callbacks, but you give up some control over the load operation.

No comments:

Post a Comment