2012-06-20 19 views
6

Necesito descargar archivos> 500 Mo con AFNetworking. A veces, el tiempo para descargarlos es> 10 minutos y si la aplicación está en segundo plano, la descarga no puede completarse.AFNetworking + grandes archivos de descarga + reanudar descargas

Así que quiero intentar descargas parciales. Encontré muchos enlaces y esto parece ser posible con los métodos pause() y resume() en AFHTTPRequestOperation.

En realidad, lo hice:

[self.downloadOperation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ 
    // Clean up anything that needs to be handled if the request times out 
    [self.downloadOperation pauseDownload]; 
    }]; 

DownloadOperation es una subclase de AFHTTPRequestOperation (Singleton).

Y en AppDelegate:

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    // resume will only resume if it's paused... 
    [[DownloadHTTPRequestOperation sharedOperation] resumeDownload]; 
} 

El servidor está bien obtener la nueva gama de cabeceras ...

Mis preguntas:

1) es-t la buena manera de hacerlo eso ? 2) ¿El currículum necesita cambiar el outputStream (anexar: NO => append: YES)? ¿O lo está gestionado en algún lugar por AFNetworking? (No se encuentra)

self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES]; 

Algo como esto (en DownloadHTTPRequestOperation):

- (void)pauseDownload 
{ 
    NSLog(@"pause download"); 
    [self pause]; 
} 

- (void)resumeDownload 
{ 
    NSLog(@"resume download"); 
    self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES]; 
    [self resume]; 
} 

Gracias por su ayuda.

Respuesta

1

Terminé usando el marco anterior (sin ARC) ASIHTTPRequest para una tarea similar. AllowResumeForFileDownloads hace lo que necesita. Tenga en cuenta que su servidor necesita admitir la reanudación leyendo el encabezado http Range.

if (![[NSFileManager defaultManager] fileExistsAtPath:downloadPath]){ 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request setDelegate:self]; 
    [request setAllowResumeForFileDownloads:YES]; 
    [request setDownloadDestinationPath:downloadPath]; 
    [request setTemporaryFileDownloadPath:tmpPath]; 
    [request startAsynchronous]; 
} 
+0

Por cierto AFNetworking es también "no ARC" – pahan

+1

AFNetworking es ARC habilitado ahora. – tangqiaoboy

Cuestiones relacionadas