Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking + Pause/ Resume downloading big files

I need to download large .zip files (up to 800 MB) with my iPad app. I want to resume the download again if it is canceled or if the app is in the background.

operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:YES];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){

// unzip the .zip


}failure:^(AFHTTPRequestOperation *operation, NSError *error){

//Handle the error

}];


[operation setDownloadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten,long long totalBytesExpectedToWrite) {

//update the progressView

}];

[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{

// What I have to do here?

}];

[operation start];
[operation waitUntilFinished];


-(void)applicationWillResignActive:(UIApplication *)application{

// What I have to do here?

}

Thanks for your help.

like image 617
L0rdShrek Avatar asked Aug 29 '12 08:08

L0rdShrek


People also ask

Can I pause and Resume downloads?

Chosen solution You should be able to right click on the download and click Pause you can then click Resume later.

Can I pause a download and continue next day?

Press Ctrl + J or click the Options dropdown menu and select Downloads to open the download manager. In the list of downloads, find the failed item and click Resume. If everything goes to plan, your download will resume from where it got interrupted.

How do I pause a download on my laptop?

If you close your Internet connection or restart your computer after pausing a download process, Windows automatically resumes the download process the next time you are connected to the Internet. During the download process, click the icon that is displayed in the notification area, and then click Pause.


1 Answers

You can use AFDownloadRequestOperation to do this.

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"....zip"]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"....zip"];
    AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", path);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
        NSLog(@"Operation%i: bytesRead: %d", 1, bytesRead);
        NSLog(@"Operation%i: totalBytesRead: %lld", 1, totalBytesRead);
        NSLog(@"Operation%i: totalBytesExpected: %lld", 1, totalBytesExpected);
        NSLog(@"Operation%i: totalBytesReadForFile: %lld", 1, totalBytesReadForFile);
        NSLog(@"Operation%i: totalBytesExpectedToReadForFile: %lld", 1, totalBytesExpectedToReadForFile);
    }];
    [operations addObject:operation];

After you restart your app, and generate the request having a same url, it will resume downloading. "shouldResume:YES" works

like image 144
Wayne Avatar answered Oct 05 '22 20:10

Wayne