AFNetworking + large files for download + download summary

I need to download files> 500 Mo with AFNetworking. Sometimes their download time is> 10 minutes, and if the application is in the background, the download cannot be completed.

So, I want to try partial download. I found a lot of links, and this seems possible using the pause () and resume () methods in AFHTTPRequestOperation.

Actually, I did:

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

DownloadOperation is a subclass of AFHTTPRequestOperation (singleton).

And in AppDelegate:

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

Server ok to get new range in headers ...

My questions:

1) Isn’t a good way to do this? 2) Is a summary required to change outputStream (add: NO => append: YES)? Or is it somewhere controlled by AFNetworking? (can not found)

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

Something like this (in DownloadHTTPRequestOperation):

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

Thanks for your help.

+6
source share
2 answers

Update:

Since steipete may no longer support AFDownloadRequestOperation ( https://github.com/steipete/AFDownloadRequestOperation/pull/68 ). NSURLSessionDownloadTask may be a better choice.


https://github.com/steipete/AFDownloadRequestOperation

In addition, I am writing the lib base on AFDownloadRequestOperation: https://github.com/BB9z/RFDownloadManager

+6
source

As a result, I used the old (not ARC) ASIHTTPRequest for a similar task. AllowResumeForFileDownloads does what you need. Note that the server must support resume by reading the HTTP range header.

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

Source: https://habr.com/ru/post/918577/


All Articles