How to run a long task in backgroung in iOS applications?

Posted by John Canady on Stack Overflow See other posts from Stack Overflow or by John Canady
Published on 2010-12-23T08:41:50Z Indexed on 2010/12/23 8:54 UTC
Read the original article Hit count: 214

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk-4.0