2012-06-20 8 views
6

Ich brauche Dateien> 500 Mo mit AFNetworking herunterladen. Manchmal ist die Zeit, um sie herunterzuladen,> 10 Minuten und wenn die App im Hintergrund ist, kann der Download nicht abgeschlossen sein.AFNetworking + große Download-Dateien + Lebenslauf Downloads

Also ich möchte teilweise Downloads versuchen. Ich habe viele Links gefunden und dies scheint mit den Methoden pause() und resume() in AFHTTPRequestOperation möglich zu sein.

Eigentlich habe ich:

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

DownloadOperation eine Unterklasse von AFHTTPRequestOperation ist (Singleton).

Und in AppDelegate:

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

Der Server ist OK, um den neuen Bereich in Header zu bekommen ...

Meine Fragen:

1) Ist-t die gute Art und Weise zu tun es? 2) Muss der Lebenslauf den outputStream ändern (append: NO => append: YES)? Oder ist es irgendwo von AFNetworking gemanagt? (Nicht finden)

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

Etwas wie folgt aus (in DownloadHTTPRequestOperation):

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

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

Vielen Dank für Ihre Hilfe.

Antwort

1

Ich endete mit dem alten (nicht ARC) ASIHTTPRequest Framework für eine ähnliche Aufgabe. AllowResumeForFileDownloads tut, was Sie brauchen. Beachten Sie, dass Ihr Server die Fortsetzung unterstützen muss, indem Sie den HTTP-Header Range lesen.

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

BTW AFNetworking ist auch "non ARC" – pahan

+1

AFNetworking ist ARC aktiviert jetzt:


https://github.com/steipete/AFDownloadRequestOperation

Auch ich eine lib Basis auf AFDownloadRequestOperation schreiben. – tangqiaoboy