views:

529

answers:

2

Actually I am working for multitasking and facing error please assist me, as I need to work on background application and also in foreground application

 - (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication*    app = [UIApplication sharedApplication];

    // Request permission to run in the background. Provide an
    // expiration handler in case the task runs long.
    NSAssert(bgTask == UIBackgroundTaskInvalid, nil);

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        // Synchronize the cleanup call on the main thread in case
        // the task actually finishes at around the same time.
        dispatch_async(dispatch_get_main_queue(), ^{
            if (bgTask != UIBackgroundTaskInvalid)
            {
                [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.

        // Synchronize the cleanup call on the main thread in case
        // the expiration handler is fired at the same time.
        dispatch_async(dispatch_get_main_queue(), ^{
            if (bgTask != UIBackgroundTaskInvalid)
            {
                [app endBackgroundTask:bgTask];
                bgTask = UIBackgroundTaskInvalid;
            }
        });
    });
}
A: 

check out http://bit.ly/d1m5Wz, if you have a developer's account. we can't really answer the os4.0 question because it's under nda, but all the information you'll need to answer that question is on that page (once logged in).

Chunjai
Friend I have copied this piece of code from specified link, but facing error in executing it, and how can I define bgtask as it is showing i.e bgtask is not in structure or union.
+1  A: 

Put in your @interface:

UIBackgroundTaskIdentifier bgTask;

(out from NDA now).

Paul Lynch