Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

beginBackgroundTaskWithExpirationHandler

I am making an application where audio will play in the background. In the following code, bgTask is undeclared. What kind of object should bgTask be?

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
    UIApplication  *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
        [app endBackgroundTask:bgTask]; 
        bgTask = UIBackgroundTaskInvalid;
    }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{});

    [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid;
}
like image 877
sidhu Avatar asked Oct 06 '10 09:10

sidhu


2 Answers

You need to declare bgTask before you assign:

UIBackgroundTaskIdentifier bgTask = 0;
like image 158
willcodejavaforfood Avatar answered Nov 06 '22 06:11

willcodejavaforfood


I cannot comment on previous answers, however to initialize UIBackgroundTaskIdentifier you should not set it to nil or 0. You should set it to:

UIBackgroundTaskInvalid

In the documentation UIBackgroundTaskInvalid says it should be used to initialize variables or to check for errors.

like image 10
Bontarest Avatar answered Nov 06 '22 05:11

Bontarest