2011-11-08 12 views
7

Estoy trabajando en un problema donde tengo que descargar alrededor de 10 archivos grandes diferentes en una cola, y necesito mostrar una barra de progreso que indica el estado de la transferencia total. Tengo esto funcionando bien con ASIHTTPRequest en iOS4, pero estoy tratando de hacer la transición a AFNetworking ya que ASIHTTPRequest tiene problemas en iOS5 y ya no se mantiene.Cambiar a AFNetworking desde ASIHTTPRequest - Problema con ASINetworkQueue

Sé que puede informar el progreso de las solicitudes individuales utilizando la descarga de descarga de AFHTTPRequestOperation, pero parece que no puedo encontrar una manera de informar el progreso general de varias solicitudes que se ejecutarían en el mismo NSOperationQueue.

¿Alguna sugerencia? ¡Gracias!

Respuesta

0

Intentaría subclasificar UIProgressView con una subclase que realiza un seguimiento de todos los diferentes elementos que está viendo y luego tiene la lógica que agrega el progreso de todos juntos.

Con este código como tal:

@implementation customUIProgressView 

-(void) updateItem:(int) itemNum ToPercent:(NSNumber *) percentDoneOnItem { 
    [self.progressQueue itemAtIndexPath:itemNum] = percentDoneOnItem; 

    [self updateProgress]; 
} 
-(void) updateProgress { 
    float tempProgress = 0; 
    for (int i=1; i <= [self.progressQueue count]; i++) { 
    tempProgress += [[self.progressQueue itemAtIndexPath:itemNum] floatValue]; 
    } 
    self.progress = tempProgress/[self.progressQueue count]; 
} 
+0

Esta es una mala práctica de codificación para MVC patrones de diseño. –

+0

Quizás. Es la herramienta adecuada para el trabajo de contenido de tamaño dinámico. Se mostrará con precisión el progreso sin necesidad de saber el tamaño del contenido primero. Encontrar el tamaño de archivos grandes (como videos o fotos) puede ser una operación costosa. – clreina

0

Usted puede subclase AFURLConnectionOperation que tiene 2 nuevas propiedades: (NSInteger)totalBytesSent, y (NSInteger)totalBytesExpectedToSend. Debe establecer estas propiedades en la devolución de llamada NSURLConnection así:

- (void)connection:(NSURLConnection *)__unused connection 
    didSendBodyData:(NSInteger)bytesWritten 
totalBytesWritten:(NSInteger)totalBytesWritten 
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite 
{ 
    [super connection: connection didSendBodyData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite]; 
    self.totalBytesSent = totalBytesWritten; 
    self.totalBytesExpectedToSend = totalBytesExpectedToSend; 
} 

su bloque uploadprogress puede tener este aspecto:

……(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { 
    NSInteger queueTotalExpected = 0; 
    NSInteger queueTotalSent  = 0; 
    for (AFURLConnectionOperation *operation in self.operationQueue) { 
     queueTotalExpected += operation.totalBytesExpectedToSend; 
     queueTotalSent  += operation.totalBytesSent; 
    } 
    self.totalProgress = (double)queueTotalSent/(double)queueTotalExpected; 
}]; 
1
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { 
    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite); 
}]; 

funcionamiento está AFHTTPRequestOperation

Cuestiones relacionadas