Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
643 views
in Technique[技术] by (71.8m points)

objective c - AFNetworking not resuming download

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

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

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];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...