我正在使用 AFNetworking 将大文件下载到我的 iPad 应用程序中。
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]; }
最佳答案
花了一些时间后,我想出了如何暂停和恢复下载。
AFNetworking 有 extensions其中之一是 AFDownloadRequestOperation本质上是用来处理大文件的暂停和恢复的。因此,这里不使用 AFHTTPRequestOperation,而是使用 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]; }
关于objective-c - AFNetworking 不恢复下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12563753/