6

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];
}

ご協力いただきありがとうございます。

4

2 に答える 2

6

アップデート:

steipete はこれ以上 AFDownloadRequestOperation を維持しない可能性があるため ( https://github.com/steipete/AFDownloadRequestOperation/pull/68 )。NSURLSessionDownloadTask の方が適している場合があります。


https://github.com/steipete/AFDownloadRequestOperation

また、AFDownloadRequestOperation の lib ベースを作成します: https://github.com/BB9z/RFDownloadManager

于 2012-08-28T07:15:37.733 に答える
1

同様のタスクに古い(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-06-27T18:30:17.177 に答える