我正在使用ASINetworkQueue排隊我的請求。我添加了12個請求,最後它下載了24次,每次下載請求兩次?這是爲什麼?我該如何預防它?Objective-C - ASINetworkQueue執行兩次添加的請求?
在呼叫進入隊列之前,我檢查已添加到隊列(NSLog(@"%@",[[self queue] operations]);)
的請求數量,它顯示12個ASIHTTP請求。
編輯:添加了我的代碼。我沒有打開準確的進度。
代碼:
- (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];
}
}
向我們展示一些代碼。 – VenoMKO
添加了相關代碼。謝謝。 –