cocoa-touch - 在 ASINetworkQueue 中使用 UIProgressView 为 ASIHTTPRequest 显示准确的进度

标签 cocoa-touch asihttprequest uiprogressview

摘要:我想使用表格 View 单元格内的进度条跟踪文件下载的进度。
我在 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* 中的错误吗?

最佳答案

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

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

关于cocoa-touch - 在 ASINetworkQueue 中使用 UIProgressView 为 ASIHTTPRequest 显示准确的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6546287/

相关文章:

objective-c - iPad/iPhone 旋转时旋转 subview

iphone - 在 XCode 4 中安装 ASIHTTPRequest

iphone - 为什么我的 UITableViewController 没有被填充在这里?

ios - 带有自定义 ProgressImage 的 UIProgressView 拉伸(stretch)不正确。怎么修?

ios - nsarray 有很多 url,在 progressview 上加载

iphone - UITableViewCell 显示 UIImageView 或 UIProgressView

iphone - 获取强烈捕获对象的 ARC 警告

ios - map 上的多个位置(使用 MKMapItem 和 CLGeocoder)

objective-c - IOS免费贴图组件

iphone - ASIHTTP请求重定向