AFNetworking +大型下载文件+简历下载 [英] AFNetworking + big download files + resume downloads
问题描述
我需要使用AFNetworking下载文件> 500 Mo。
有时候下载的时间是10分钟,如果应用程序是在后台,下载是无法完成的。
所以我想尝试部分下载。我发现很多链接,这似乎可以通过在AFHTTPRequestOperation上的pause()和resume()方法。
其实我做了:
[self.downloadOperation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^ {
//清理任何需要处理的事情,如果请求超时
[ self.download操作pauseDownload];
}];
DownloadOperation是AFHTTPRequestOperation(singleton)的子类。
在AppDelegate中:
- (void)applicationWillEnterForeground:(UIApplication *)应用程序
{
//如果暂停,恢复只会恢复...
[[DownloadHTTPRequestOperation sharedOperation] resumeDownload];
}
服务器可以在头文件中获取新的范围...
我的问题:
1)是不是最好的方法?
2)简历是否需要更改outputStream(附加:NO => append:YES)?还是由AFNetworking管理? (不找到)
self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
这样的东西(在DownloadHTTPRequestOperation中):
- (void)pauseDownload
{
NSLog(@pause download);
[self pause];
}
- (void)resumeDownload
{
NSLog(@resume download);
self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
[self resume];
}
感谢您的帮助。
更新:
由于steipete可能不会再维护AFDownloadRequestOperation( https://github.com/steipete/AFDownloadRequestOperation/pull/68 )。 NSURLSessionDownloadTask可能是一个更好的选择。
https://github.com/steipete/AFDownloadRequestOperation
另外,我在AFDownloadRequestOperation上编写一个库: https://github.com/BB9z/RFDownloadManager
I need to download files > 500 Mo with AFNetworking. Sometimes, the time to download them is > 10 minutes and if the app is in background, the download can't be complete.
So I want to try partial downloads. I found a lot of links and this seems to be possible with pause() and resume() methods on 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's paused...
[[DownloadHTTPRequestOperation sharedOperation] resumeDownload];
}
The server is OK to get the new range in headers...
My questions:
1) Is-t the good way to do it ? 2) Does the resume needs to change the outputStream (append:NO => append:YES) ? Or is-it managed somewhere by AFNetworking ? (don't find)
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.
Update:
As steipete may wont maintain AFDownloadRequestOperation any more (https://github.com/steipete/AFDownloadRequestOperation/pull/68). NSURLSessionDownloadTask may be a better choice.
https://github.com/steipete/AFDownloadRequestOperation
Also, I write a lib base on AFDownloadRequestOperation: https://github.com/BB9z/RFDownloadManager
这篇关于AFNetworking +大型下载文件+简历下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!