我正在使用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]; }
答案 0 :(得分:6)
花了一些时间后,我想出了如何暂停和恢复下载。
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]; }