Download AFX2B;大下载文件+;恢复下载
我需要使用AFN网络下载大于500 Mo的文件。 有时,下载它们的时间超过10分钟,如果应用程序在后台,下载就无法完成 所以我想尝试部分下载。我发现了很多链接,这似乎可以通过AFHTTPRequestOperation上的pause()和resume()方法实现 事实上,我做到了:Download AFX2B;大下载文件+;恢复下载,download,afnetworking,resume,Download,Afnetworking,Resume,我需要使用AFN网络下载大于500 Mo的文件。 有时,下载它们的时间超过10分钟,如果应用程序在后台,下载就无法完成 所以我想尝试部分下载。我发现了很多链接,这似乎可以通过AFHTTPRequestOperation上的pause()和resume()方法实现 事实上,我做到了: [self.downloadOperation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ // Clean up anythi
[self.downloadOperation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
// Clean up anything that needs to be handled if the request times out
[self.downloadOperation pauseDownload];
}];
DownloadOperation是AFHTTPRequestOperation(singleton)的一个子类
在AppDelegate中:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// resume will only resume if it's paused...
[[DownloadHTTPRequestOperation sharedOperation] resumeDownload];
}
服务器可以在标题中获取新范围
我的问题是:
1) 这是好办法吗?
2) 简历是否需要更改输出流(append:NO=>append:YES)?还是由AFN网络管理?(找不到)
类似这样的内容(在下载的HttpRequestOperation中):
感谢您的帮助。我最终使用了旧的(非ARC)框架来完成类似的任务。AllowResumeForFileDownloads可以满足您的需要。请注意,您的服务器需要通过读取范围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];
}
更新:
因为Steipet可能不再维护AFDownloadRequestOperation()。NSURLSessionDownloadTask可能是更好的选择
此外,我还基于AFDownloadRequestOperation编写了一个库:
- (void)pauseDownload
{
NSLog(@"pause download");
[self pause];
}
- (void)resumeDownload
{
NSLog(@"resume download");
self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
[self resume];
}
if (![[NSFileManager defaultManager] fileExistsAtPath:downloadPath]){
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setAllowResumeForFileDownloads:YES];
[request setDownloadDestinationPath:downloadPath];
[request setTemporaryFileDownloadPath:tmpPath];
[request startAsynchronous];
}