Objective-C – ASINetworkQueue执行添加的请求两次?
我正在使用 ASINetworkQueue 对我的请求进行排队。而我添加了 12 个请求,最终它下载了 24 次,每个请求下载两次?这是为什么?我该如何预防呢?
在调用队列上的 go 之前,我检查已添加到队列中的请求数 (NSLog(@"%@",[[self queue] actions]);)
并显示 12 个 ASIHTTPRequest。
编辑:添加了我的代码。我没有打开准确进度。
代码:
- (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'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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我注意到我正在为队列和单个请求设置委托,这就是请求下载两次的原因。
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.
引用文档:
这将导致队列中的每个条目都有两个 HTTP 请求,因此它也许可以解释您所看到的内容?
To quote the docs:
This would result in two HTTP requests for every entry in the queue, so it perhaps explains what you're seeing?