在ASINetworkQueue中使用UIProgressView for ASIHTTPRequest显示准确的进度

时间:2011-07-01 09:44:32

标签: cocoa-touch asihttprequest uiprogressview

摘要:我想在tableview的单元格内跟踪进度条的文件下载进度。 我在ASINetworkQueue中使用ASIHTTPRequest来处理下载 它有效,但进度条保持在0%,并在每次下载结束时直接跳至100%。


详细信息: 我以这种方式设置ASIHTTPRequest请求和ASINetworkQueue:

[只是我的代码摘录]

- (void) startDownloadOfFiles:(NSArray *) filesArray {

    for (FileToDownload *aFile in filesArray) {

        ASIHTTPRequest *downloadAFileRequest = [ASIHTTPRequest requestWithURL:aFile.url];

        UIProgressView *theProgressView = [[UIProgressView alloc] initWithFrame:CGRectMake(20.0f, 34.0f, 280.0f, 9.0f)];
        [downloadAFileRequest setDownloadProgressDelegate:theProgressView];

        [downloadAFileRequest setUserInfo:
            [NSDictionary dictionaryWithObjectsAndKeys:aFile.fileName, @"fileName",
                                                        theProgressView, @"progressView", nil]];
        [theProgressView release];

        [downloadAFileRequest setDelegate:self];
        [downloadAFileRequest setDidFinishSelector:@selector(requestForDownloadOfFileFinished:)];
        [downloadAFileRequest setDidFailSelector:@selector(requestForDownloadOfFileFailed:)];
        [downloadAFileRequest setShowAccurateProgress:YES];

        if (! [self filesToDownloadQueue]) {
            // Setting up the queue if needed
            [self setFilesToDownloadQueue:[[[ASINetworkQueue alloc] init] autorelease]];

            [self filesToDownloadQueue].delegate = self;
            [[self filesToDownloadQueue] setMaxConcurrentOperationCount:2];
            [[self filesToDownloadQueue] setShouldCancelAllRequestsOnFailure:NO]; 
            [[self filesToDownloadQueue] setShowAccurateProgress:YES]; 

        }

        [[self filesToDownloadQueue] addOperation:downloadAFileRequest];
    }        

    [[self filesToDownloadQueue] go];
}

然后,在UITableViewController中,我创建单元格,并使用存储在请求的userInfo字典中的对象添加文件名和UIProgressView。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"fileDownloadCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"FileDownloadTableViewCell" owner:self options:nil];
        cell = downloadFileCell;
        self.downloadFileCell = nil;
    }

    NSDictionary *userInfo = [self.fileBeingDownloadedUserInfos objectAtIndex:indexPath.row];

    [(UILabel *)[cell viewWithTag:11] setText:[NSString stringWithFormat:@"%d: %@", indexPath.row, [userInfo valueForKey:@"fileName"]]];

    // Here, I'm removing the previous progress view, and adding it to the cell
    [[cell viewWithTag:12] removeFromSuperview];
    UIProgressView *theProgressView = [userInfo valueForKey:@"progressView"];
    if (theProgressView) {
        theProgressView.tag = 12;
        [cell.contentView addSubview:theProgressView];
    } 


    return cell;
}

进度条全部添加,进度设置为0%。 然后,在下载结束时,他们立即跳到100%。

部分下载非常大(超过40Mb)。

我不会对线程做任何棘手的事。

阅读ASIHTTPRequest的论坛,似乎我并不孤单,但我找不到解决方案。 我错过了一些明显的东西吗这是ASI *中的错误吗?

1 个答案:

答案 0 :(得分:6)

ASIHTTPRequest只能在服务器发送Content-Length:标头时报告进度,否则它不知道响应有多大。 (ASINetworkQueue也在开始时发送HEAD请求以试图找出文档大小。)

尝试使用charlesproxy或wireshark收集所有网络流量,查看这些标头是否存在和/或HEAD请求发生了什么。