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

Gui*_*ume 7 cocoa-touch uiprogressview asihttprequest

简介:我想通过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];
}
Run Code Online (Sandbox Code Playgroud)

然后,在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;
}
Run Code Online (Sandbox Code Playgroud)

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

一些下载非常大(超过40Mb).

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

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

Jos*_*phH 6

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

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