2012-06-20 85 views
6

我需要下载文件> 500 Mo与AFNetworking。 有时,下载它们的时间大于10分钟,如果应用程序在后台,则下载无法完成。AFNetworking +大的下载文件+恢复下载

所以我想尝试部分下载。我发现了很多链接,并且这似乎可以通过AFHTTPRequestOperation上的pause()和resume()方法来实现。

其实,我所做的:

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

DownloadOperation是AFHTTPRequestOperation(单身)的子类。

而且在AppDelegate中:

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

服务器即可得到头的新范围...

我的问题:

1)是-T的好办法吗它呢? 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]; 
} 

感谢您的帮助。

回答

1

我最终使用旧的(非ARC)ASIHTTPRequest框架进行类似的任务。 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]; 
} 
+0

BTW AFNetworking也是 “非ARC” – pahan

+1

AFNetworking现在启用ARC。 – tangqiaoboy