2012-09-24 83 views
4

我正在使用AFNetworking將大文件下載到我的iPad應用程序中。AFNetworking不恢復下載

AFHTTPRequestOperation的一個實例用於下載此文件。下面是引用代碼 -

 
//request is the NSRequest object for the file getting downloaded 
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request 
             success:^(AFHTTPRequestOperation *operation, id responseObject) {                   

             } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 


             }]; 
//here path variable is the location where file would be placed on download 
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path 
                   append:YES]; 
//since this class is subclass of AFHTTPClient so the operation is added to request queue 
[self enqueueHTTPRequestOperation:operation]; 

現在,這裏的問題是,當我嘗試暫停使用下面的功能恢復此下載後,pauseDownload功能的正常不過的簡歷下載不起作用的方式應該和似乎下載從一開始就開始,因爲我期待它會從它離開的地方恢復。這可能是一個問題?

 
-(void)pauseDownload{ 
    [operation pause]; 
} 

-(void)resumeDownload{ 
    [operation resume]; 
} 

回答

6

花了一段時間後,我想出瞭如何暫停和恢復下載。

AFNetworking有extensions其中之一是AFDownloadRequestOperation它主要用於處理大文件的暫停和恢復。因此,不要使用AFDTPRequestOperation,而要使用AFDownloadRequestOperation。下面是示例代碼

 
//request is the NSRequest object for the file getting downloaded and targetPath is the final location of file once its downloaded. Don't forget to set shouldResume to YES 
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request 
                        targetPath:targetPath 
                        shouldResume:YES]; 
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    //handel completion 
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    //handel failure 
}]; 
[operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) { 
    //handel progress 

}]; 
//since this class is subclass of AFHTTPClient so the operation is added to request queue 
[self enqueueHTTPRequestOperation:operation]; 

//used to pause the download 
-(void)pauseDownload{ 
    [operation pause]; 
} 
//used to resume download 
-(void)resumeDownload{ 
    [operation resume]; 
} 
+2

當應用程序已退出並重新啓動時,這也起作用嗎? – openfrog

+0

@openfrog是的,它也適用於應用程序已退出並重新啓動 –

+2

可以請你幫我在這 [self enqueueHTTPRequestOperation:operation]; –