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
connectionDidFinishLoadingordidFailWithErrorstart 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: