I'm using an ASINetworkQueue to queue up my requests. Whereas I add 12 requests, in the end it downloads 24 times, downloading each request twice? Why is that? And how can I prevent it?
Before calling go on the queue I check how many requests that have been added to the queue (NSLog(@"%@",[[self queue] operations]);)
and it displays 12 ASIHTTPRequests.
EDIT: Added my code. I'm not having accurate progress turned on.
Code:
- (void)setupQueue {
DLog(@"setupQueue running");
// Stop anything already in the queue before removing it
[[self queue] cancelAllOperations];
// Creating a new queue each time we use it means we don't have to worry about clearing delegates or resetting progress tracking
[self setQueue:[ASINetworkQueue queue]];
[[self queue] setDelegate:self];
[[self queue] setRequestDidStartSelector:@selector(requestStarted:)];
[[self queue] setRequestDidFinishSelector:@selector(requestFinished:)];
[[self queue] setRequestDidFailSelector:@selector(requestFailed:)];
[[self queue] setQueueDidFinishSelector:@selector(queueFinished:)];
}
- (NSArray *)parseMapsThumbsUrls {
DLog(@"parseMapsThumbsUrls running");
NSDictionary *results = [[self officesJSON] JSONValue];
NSArray *offices = [results objectForKey:@"offices"];
NSMutableArray *mapsThumbsUrls = [[[NSMutableArray alloc] init] autorelease];
for (NSDictionary *office in offices) {
NSString *mapThumbImageString = [NSString stringWithFormat:@"http://maps.google.com/maps/api/staticmap?zoom=11&markers=color:0xFF7300|%@,%@&size=70x70&sensor=true", [office objectForKey:@"latitude"], [office objectForKey:@"longitude"]];
// Make the string HTML-compatible
NSString *url = [mapThumbImageString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[mapsThumbsUrls addObject:url];
}
return mapsThumbsUrls; // returns an array of 6 urls
}
- (void)downloadMapsThumbs {
DLog(@"downloadMapsThumbs running");
[self setupQueue];
NSArray *mapsThumbsUrls = [self parseMapsThumbsUrls];
for (NSString *mapThumbUrl in mapsThumbsUrls) {
NSURL *url = [NSURL URLWithString:mapThumbUrl];
ASIHTTPRequest *mapThumbRequest = [ASIHTTPRequest requestWithURL:url];
[mapThumbRequest setTag:2];
[mapThumbRequest setDelegate:self];
[[self queue] addOperation:mapThumbRequest];
}
}
- (void)download {
[self downloadMapsThumbs];
开发者_开发知识库 DLog(@"%@",[[self queue] operations]);
[[self queue] go];
}
- (void)requestFinished:(ASIHTTPRequest *)request {
if (request.tag == 2) { // Process thumbs
DLog(@"%@",[[request originalURL] description]);
// Use when fetching binary data
NSData *responseData = [request responseData];
[[self mapThumbs] addObject:responseData];
}
}
I noticed that I'm setting the delegate for both the queue and for the individual requests, that's why the requests get downloaded twice.
To quote the docs:
When you start a queue with accurate progress turned on, it will first perform a HEAD request for all the GET requests in the queue to get the total size of the data to be downloaded. Once it has the total size, it can accurately show the total progress, and the real requests will start.
This would result in two HTTP requests for every entry in the queue, so it perhaps explains what you're seeing?
精彩评论