【问题标题】:AFNetworking + big download files + resume downloadsAFNetworking + 大下载文件 + 恢复下载
【发布时间】:2012-06-20 15:48:32
【问题描述】:

我需要使用 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];
  }];

DownloadOperation 是 AFHTTPRequestOperation(单例)的子类。

在 AppDelegate 中:

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

服务器可以获取标头中的新范围...

我的问题:

1) 这样做是不是好方法? 2) 简历是否需要更改 outputStream (append: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];
}

感谢您的帮助。

【问题讨论】:

    标签: download afnetworking resume


    【解决方案1】:

    更新:

    因为 steipete 可能不再维护 AFDownloadRequestOperation (https://github.com/steipete/AFDownloadRequestOperation/pull/68)。 NSURLSessionDownloadTask 可能是更好的选择。


    https://github.com/steipete/AFDownloadRequestOperation

    另外,我写了一个基于 AFDownloadRequestOperation 的库:https://github.com/BB9z/RFDownloadManager

    【讨论】:

      【解决方案2】:

      我最终使用旧的(非 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];
      }
      

      【讨论】:

        猜你喜欢
        • 2012-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多