AFNetworking没有恢复下载 [英] AFNetworking not resuming download

查看:89
本文介绍了AFNetworking没有恢复下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AFNetworking将大文件下载到我的iPad应用中。

I am using AFNetworking to download large file into my iPad app.

AFHTTPRequestOperation的一个实例用于下载此文件。以下是供参考的代码 -

An instance of AFHTTPRequestOperation is used to download this file. Below is the code for reference -


//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功能正常工作但是简历下载不会按照它应该的方式工作,似乎下载从一开始就开始,因为我期望它将从它离开的地方恢复。这可能是个问题?

Now the issue here is that when i try pause and resume this download using below functions, the pauseDownload function works properly however the resume download doesn't work the way it should and it seems like the download starts from the beginning where as I was expecting that it will resume from the place it left. What could be an issue here?


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

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


推荐答案

花了一些时间我想出了如何暂停和恢复下载。

After spending sometime I figured out how to pause and resume the download.

AFNetworking已扩展 AFDownloadRequestOperation ,它主要用于处理大文件的暂停和恢复。因此,不使用AFHTTPRequestOperation,而是使用AFDownloadRequestOperation。下面是示例代码

AFNetworking has extensions one of them is AFDownloadRequestOperation which is essentially used to handle the pause and resume of large files. So instead of using AFHTTPRequestOperation here AFDownloadRequestOperation is to be used. Below is sample code


//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];
}

这篇关于AFNetworking没有恢复下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆