Objective-C – ASINetworkQueue执行添加的请求两次?

发布于 2024-11-28 09:01:42 字数 2732 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森罗 2024-12-05 09:01:42

我注意到我正在为队列和单个请求设置委托,这就是请求下载两次的原因。

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.

听你说爱我 2024-12-05 09:01:42

引用文档:

当您启动一个启用了精确进度的队列时,它会首先对队列中的所有 GET 请求执行 HEAD 请求,以获取要下载的数据的总大小。一旦掌握了总大小,就可以准确显示总进度,真正的请求就会开始。

这将导致队列中的每个条目都有两个 HTTP 请求,因此它也许可以解释您所看到的内容?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文