As I understand methods named *WithContentsOfURL: like [NSData dataWithContentsOfURL:] are synchronous.
So if I want to download from 3 URLs asynchronously using *WithContentsOfURL: methods I have to put them in a GCD dispatch like:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *dataOne = [NSData dataWithContentsOfURL:dataOne];
NSData *dataTwo = [NSData dataWithContentsOfURL:dataTwo];
NSData *dataThree = [NSData dataWithContentsOfURL:dataThree];
});
Is NSURLConnection using GCD “behind the scenes”? Would this be (somewhat) equivalent to the below methods in terms of asynchronous download:
NSURLRequest *myRequestOne = [NSURLRequest requestWithURL:[NSURL URLWithString:URLOne] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *myConnectionOne = [[NSURLConnection alloc] initWithRequest:myRequestOne delegate:self];
NSURLRequest *myRequestTwo = [NSURLRequest requestWithURL:[NSURL URLWithString:URLTwo] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *myConnectionThree = [[NSURLConnection alloc] initWithRequest:myRequestTwo delegate:self];
NSURLRequest *myRequestThree = [NSURLRequest requestWithURL:[NSURL URLWithString:URLThree] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *myConnectionThree = [[NSURLConnection alloc] initWithRequest:myRequestThree delegate:self];
Also what would happen if I would put a NSURLConnection inside a dispatch_async ?
They’re not really equivalent, since using NSURLConnectionDelegate allows you to react to things like request failure, authentication challenge etc.
The first example you give using GCD would work for valid URLs, but for anything else will result in no data being returned. Do as Eugene suggests and use ASIHTTPRequest – it’s much easier.