objective-c - AFNetworking 不恢复下载

标签 objective-c ios afnetworking

我正在使用 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/

相关文章:

ios - 如何在 NSUserDefaults 中存储自定义对象

ios - Swift 应用程序在 ios 8.x 上呈现黑屏

ios - Today View Extension(Widget)中的 UISlider

iphone - AFNetworking 预期内容类型错误

ios - 触摸时未调用 didSelectRowAtIndexPath

ios - 如何在 iOS 的 info.plist 中添加属性

ios - 如何使用 AFNetworking 库调用 .NET XML Web 服务

ios - 当setImageWithUrl显示缓存的图像时,白色闪烁

iphone - 下面的属性声明有什么区别?

php - 返回的 JSON MIME 类型存在问题