2012-09-24 63 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]; –