How to run a long task in backgroung in iOS applications?
- by John Canady
I am developing an application which requires running a task in background. I am trying this by calling a method which will retrieve(download) data from web server through web services. this method will call some more methods which are in different view controller classes. Here when I tap on home button of device, the method is calling but no further execution of the remaining code.
This is the code I have written in
(void)applicationDidEnterBackground:(UIApplication )application
{
UIApplication app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask = 0;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
NSString *updatekey = [[NSUserDefaults standardUserDefaults] objectForKey:@"updatesetting"];
if([updatekey isEqualToString:@"enabled"])
{
DataSettingsView *periodicUpdate = [[DataSettingsView alloc] init];
[periodicUpdate updateDataPeriodically];
//[periodicUpdate viewDidLoad];
//[periodicUpdate release];
}
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
some one please help me in this background execution of long tasks with some example of code.
Some help will appreciated and helpful to me.