ale*_*our 6 resume download afnetworking
我需要使用AFNetworking下载文件> 500 Mo. 有时,下载它们的时间大于10分钟,如果应用程序处于后台,则下载无法完成.
所以我想尝试部分下载.我找到了很多链接,这似乎可以在AFHTTPRequestOperation上使用pause()和resume()方法.
实际上,我做了:
[self.downloadOperation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
// Clean up anything that needs to be handled if the request times out
[self.downloadOperation pauseDownload];
}];
Run Code Online (Sandbox Code Playgroud)
DownloadOperation是AFHTTPRequestOperation(singleton)的子类.
在AppDelegate中:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// resume will only resume if it's paused...
[[DownloadHTTPRequestOperation sharedOperation] resumeDownload];
}
Run Code Online (Sandbox Code Playgroud)
服务器可以在标题中获取新范围...
我的问题:
1)是不是这样做的好方法?2)简历是否需要更改outputStream(追加:NO =>追加:是)?或者它是由AFNetworking管理的吗?(找不到)
self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
Run Code Online (Sandbox Code Playgroud)
像这样的东西(在DownloadHTTPRequestOperation中):
- (void)pauseDownload
{
NSLog(@"pause download");
[self pause];
}
- (void)resumeDownload
{
NSLog(@"resume download");
self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
[self resume];
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
更新:
由于steipete可能不再维护AFDownloadRequestOperation(https://github.com/steipete/AFDownloadRequestOperation/pull/68).NSURLSessionDownloadTask可能是更好的选择.
https://github.com/steipete/AFDownloadRequestOperation
另外,我在AFDownloadRequestOperation上编写了一个lib库:https://github.com/BB9z/RFDownloadManager
我最终使用旧的(非 ARC)ASIHTTPRequest框架来完成类似的任务。AllowResumeForFileDownloads可以满足您的需要。请注意,您的服务器需要支持通过读取Range http 标头来恢复。
if (![[NSFileManager defaultManager] fileExistsAtPath:downloadPath]){
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setAllowResumeForFileDownloads:YES];
[request setDownloadDestinationPath:downloadPath];
[request setTemporaryFileDownloadPath:tmpPath];
[request startAsynchronous];
}
Run Code Online (Sandbox Code Playgroud)