未调用ASINetworkQueue委托方法

时间:2012-05-22 06:43:00

标签: iphone delegates download asihttprequest network-protocols

在我的应用程序中,我使用ASINetworkQueue方法下载文件。我有n个按钮,每个按钮点击不同的队列将创建多个请求,并开始异步下载(多次下载)。它工作正常,但我无法跟踪每个队列何时完成。我用了,

        [self.myQueue cancelAllOperations];
        [self.myQueue setDownloadProgressDelegate:currentProgress];
        [self.myQueue setDelegate:self];
        [self.myQueue setShowAccurateProgress:YES];
        [self.myQueue setRequestDidFinishSelector:@selector(requestFinished:)];
        [self.myQueue setQueueDidFinishSelector:@selector(queueComplete:)];

并添加了请求,

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:str]];
                            [request setDownloadProgressDelegate:currentProgress];
                            [request setShowAccurateProgress:YES];   
                            [request setDelegate:self];
                            [request shouldContinueWhenAppEntersBackground];
                            [request allowResumeForFileDownloads];
                            [request startAsynchronous];
                            [self.myQueue addOperation:request];

最后,[self.myQueue go]; 而委托方法是

- (void)queueComplete:(ASINetworkQueue *)queue
{
   NSLog(@"Queue completed");
}

但最后没有调用它。这里出了什么问题?任何的想法? 而且,如果有多个队列同时执行,我们如何区分最终完成哪个队列?

修改

   - (void)download{
    UIImageView *image = (UIImageView *)[mainView viewWithTag:selectedTag];
    for (UIProgressView *currentProgress in [image subviews]) {
        if ([currentProgress isKindOfClass:[UIProgressView class]]) {
            NSLog(@"Prog tag: %d",currentProgress.tag);
            if(currentProgress)
            {
                currentProgress.progress = 0.0;
                [[self myQueue] cancelAllOperations];
                [self setNetworkQueue:[ASINetworkQueue queue]];
                [[self myQueue] setDownloadProgressDelegate:currentProgress];
                [[self myQueue] setDelegate:self];
                [[self myQueue] setShowAccurateProgress:YES];
                ASIHTTPRequest *request;
                [myQueue setQueueDidFinishSelector:@selector(queueComplete:)];
                for (int h = 0; h < [urlArray count]; h++) {
                    request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[urlArray objectAtIndex:h]]];
                    [[self myQueue] addOperation:request];
                }
                [[self myQueue] go];
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在将请求添加到队列之前,您正在启动请求。

[request startAsynchronous];

请求的启动应该只由队列处理。因此,删除该行,应该调用完整的回调。

如果有多个队列,则应保留对每个队列的引用。你可以将它们放在NSMutableArray中。然后在完整的回调中,您可以通过检查阵列中的队列来检查哪个队列已完成。

编辑:

您似乎忘记设置myQueue iVar,因此它是nil并忽略了所有方法调用。所以添加:

self.myQueue = [ASINetworkQueue queue];

要在数组中拥有多个队列:

self.queues = [NSMutableArray array] // queues is a retained property
ASINetworkQueue *queue = [ASINetworkQueue queue];
[queues addObject:queue];