2011-07-01 9 views
7

Resumen: Deseo hacer un seguimiento del progreso de las descargas de archivos con barras de progreso dentro de las celdas de una tabla vista. Estoy usando ASIHTTPRequest en ASINetworkQueue para manejar las descargas.
Funciona, pero las barras de progreso permanecen en 0% y saltan directamente al 100% al final de cada descarga.Progreso preciso mostrado con UIProgressView para ASIHTTPRequest en ASINetworkQueue


Detalles: He definido mis peticiones ASIHTTPRequest y ASINetworkQueue esta manera:

[solo una parte de mi código]

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

Luego, en un UITableViewController, creo celdas, y agregue el nombre del archivo y UIProgressView usando los objetos almacenados en el diccionario userInfo de la solicitud.

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

Se agregó la barra de progreso, con el progreso establecido en 0%. Luego, al final de la descarga, instantáneamente saltan al 100%.

Algunas de las descargas son muy grandes (más de 40Mb).

No hago nada complicado con los hilos.

Al leer los foros de ASIHTTPRequest, parece que no estoy solo, pero no pude encontrar una solución. ¿Me estoy perdiendo algo obvio? ¿Es esto un error en ASI *?

Respuesta

6

ASIHTTPRequest solo puede informar el progreso si el servidor está enviando Content-Length: headers, ya que de lo contrario no sabrá cuán grande será la respuesta. (ASINetworkQueue también envía solicitudes HEAD al inicio para intentar averiguar el tamaño de los documentos.)

Intente recopilar todo el tráfico de red con charlesproxy o wireshark, vea si estos encabezados están presentes y/o qué está sucediendo con las solicitudes HEAD.

+0

La barra de progreso funcionaba cuando no estaba usando ASINetworkQueue, solo ASIHTTPRequests. Así que supongo que los encabezados están bien. Verificará con el rastreo de la red. – Guillaume

+0

En los encabezados, no tengo la información de longitud del contenido. ¿Es posible que el progreso de ASIHTTPRequests esté funcionando con el mismo servidor cuando no se utiliza una cola? Le pedí al desarrollador web que cambiara los encabezados enviados. Veremos como va. – Guillaume

+0

No estoy seguro si eso es posible. La única forma de saberlo con certeza sería probar tu código anterior y verificar los encabezados allí también. – JosephH

Cuestiones relacionadas