我正在使用ASINetworkQueue来排队我的请求。我添加12个请求,最后下载24次,每次下载两次请求?这是为什么?我该如何预防?
在调用队列之前,我会检查已添加到队列(NSLog(@"%@",[[self queue] operations]);)
的请求数量,并显示12个ASIHTTPRequests。
编辑:添加了我的代码。我没有准确的进展。
代码:
- (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];
}
}
答案 0 :(得分:2)
我注意到我正在为队列和各个请求设置委托,这就是请求被下载两次的原因。
答案 1 :(得分:1)
引用文档:
当您启动准确进度的队列时,它将首先对队列中的所有GET请求执行HEAD请求,以获取要下载的数据的总大小。一旦它具有总大小,它就可以准确地显示总进度,并且真正的请求将开始。
这会导致队列中每个条目的两个HTTP请求,所以它可能解释了你所看到的内容?