Accurate progress displayed using UIProgressView for ASIHTTPRequest in ASINetworkQueue

Summary: I want to track the progress of downloading files with progress bars inside the table cells. I am using ASIHTTPRequest in ASINetworkQueue to handle downloads.
It works, but the progress indicators remain at 0% and immediately go to 100% at the end of each download.


Details: I configured my ASIHTTPRequest and ASINetworkQueue requests as follows:

[Only extracting my code]

- (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]; } 

Then, in the UITableViewController, I create the cells and add the file name and UIProgressView using the objects stored in the userInfo dictionary of the request.

 - (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; } 

A progress bar has been added, with progress reaching 0%. Then, at the end of the download, they instantly switch to 100%.

Some downloads are very large (over 40 MB).

I do not do anything complicated in threads.

Reading the ASIHTTPRequest forums, it seems I am not alone, but I could not find a solution. Am I missing something? Is this a bug in ASI *?

+7
source share
1 answer

ASIHTTPRequest can only report progress if the server sends Content-Length: headers, because otherwise it does not know how large the response will be. (ASINetworkQueue also sends HEAD requests at the beginning to try to determine the size of the document.)

Try to collect all network traffic using charlesproxy or wirehark, see if these headers are present and / or what happens with HEAD requests.

+6
source

All Articles