据我所知,名为*WithContentsOfURL:
的方法如[NSData dataWithContentsOfURL:]
是同步的。Objective-C - 使用GCD和NSURLConnection的后台线程
所以,如果我想从3个网址下载使用异步方法*WithContentsOfURL:
我已经把它们放在一个GCD调度,如:使用GCD
“幕后”
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];
});
是NSURLConnection
?这将是(有点),相当于下面的方法在异步下载方面:
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];
另外,如果我把一个NSURLConnection
一个dispatch_async
内会发生什么?