I am working with Afnetworking with my project and i want to download big file around 100mb or 150mb in background but in apple documentation they said that background task will last upto 10 mins so how should i solve this issue ?
I search on internet and i idle timer to disable it or set timer according to the file?( but how can i set idle timer according to my file size and it also depends on internet connection)..
In some SO post matte post this answer but i am not sure that he disable the idle timmer for this afnetworking background answer
(void)applicationWillResignActive:(UIApplication *)application {
__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
[application endBackgroundTask:backgroundTaskIdentifier];
[[YourRestClient sharedClient] cancelAllHTTPOperations];
}];
this is my afnetworking code
NSURL *url = [NSURL URLWithString:downloadMainURL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient.operationQueue setMaxConcurrentOperationCount:1];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:downloadPostUrl parameters:postRequest];
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:destPath shouldResume:YES];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if(operation.response.statusCode == 200) {
NSLog(@"enable ipad sleep");
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
[loadingHUD hide:TRUE];
// NSLog(@"responseString is %@",[operation responseString]);
}
else{
NSLog(@"setCompletionBlockWithSuccess");
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
[loadingHUD hide:TRUE];
}
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if(operation.response.statusCode!= 200) {
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
[loadingHUD hide:TRUE];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Attention" message:@"download failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}];
[httpClient enqueueHTTPRequestOperation:operation];
[operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
{
int filesize = [[NSUserDefaults standardUserDefaults] integerForKey:@"filesize"];
int totalbytes=filesize+bytesWritten;
NSLog(@"productidFileSize value is %f",productidFileSize);
NSLog(@"totalBytesWritten value is %d",totalbytes);
progress = ((float)totalbytes )/ productidFileSize;
NSLog(@"progress value is %f",progress);
loadingHUD.progress = progress;
[[NSUserDefaults standardUserDefaults] setInteger:totalbytes forKey:@"filesize"];
}];
}
}