如何使用GCD的NSURLRequest队列? [英] How to make a queue of NSURLRequest using GCD?
问题描述
我有一个 NSMutableArray
包含7个互联网 URL
,从中我需要抓住 HTTP
头。
我使用这些方法来使异步
所有工作完美):
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response $ b $ (NSURLConnection *)连接didFailWithError:(NSError *)错误
- (void)connectionDidFinishLoading(NSURLConnection *)连接didReceiveData:(NSData *)data
- :(NSURLConnection *)connection
问题是我需要下载每个 URL
遵循 NSMutableArray
的顺序,但由于异步
连接的性质订单搞乱了。
我不想使用同步
连接,因为它们阻塞了主线程。
如何使用
以确保下载将遵循我的 GCD
> Main Thread NSMutableArray
的索引从0到6的顺序,其中包含7 URL
感谢您的帮助!
你不需要GCD。您可以从第一次下载开始,并在 connectionDidFinishLoading
或 didFailWithError
中开始下一次下载。您只需要保留当前下载的索引,以便您知道是否已完成或下一步要开始下载。
以下只是一个草图这个想法:
//开始第一个下载:
self.currentDownload = 0;
request = [NSURLRequest requestWithURL:[self.myURLArray objectAtIndex:0]];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
//和在connectionDidFinishLoading / didFailWithError:
self.currentDownload ++;
if(self.currentDownload< self.myURLArray.count){
request = [NSURLRequest requestWithURL:[self.myURLArray objectAtIndex:self.currentDownload]];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
} else {
//所有下载完成。
}
I have a NSMutableArray
containing 7 internet URLs
from which I need to grab the HTTP
headers.
I'm using these methods to make asynchronous
connections (and all works perfectly):
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
The problem is that I need to download each URL
following the order of the NSMutableArray
, but due to the nature of the asynchronous
connection the order gets messed up.
I don't want to use synchronous
connections because they block the Main Thread.
How do I make a queue using GCD
on the Main Thread
to ensure that the downloads will follow the order of indexes from 0 to 6 of my NSMutableArray
containing the 7 URLs
?
Thanks for your help!
You do not need GCD for that. You can start with the first download, and in connectionDidFinishLoading
or didFailWithError
start the next download. You would only need to maintain an index of the current download, so that you know if you are finished or which download to start next.
The following is just a sketch of this idea:
// Start the first download:
self.currentDownload = 0;
request = [NSURLRequest requestWithURL:[self.myURLArray objectAtIndex:0]];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
// and in connectionDidFinishLoading/didFailWithError:
self.currentDownload++;
if (self.currentDownload < self.myURLArray.count) {
request = [NSURLRequest requestWithURL:[self.myURLArray objectAtIndex:self.currentDownload]];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
} else {
// All downloads finished.
}
这篇关于如何使用GCD的NSURLRequest队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!