开发者

beginBackgroundTaskWithExpirationHandler

开发者 https://www.devze.com 2023-01-19 08:52 出处:网络
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?

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:(UIApplicat开发者_开发问答ion *)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;
}


You need to declare bgTask before you assign:

UIBackgroundTaskIdentifier bgTask = 0;


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.


You are using the bgTask in the block so you have to declare it like this..

__block UIBackgroundTaskIdentifier bgTask = 0;

__block will allow this variable to use in the block method. And its have double underscore.

0

精彩评论

暂无评论...
验证码 换一张
取 消