EndBackgroundTask fails: background task does not exist with id 4, or it may have already been completed

By implementing a timer in the background as follows and using Xcode 4.5.2

UIDevice *device = [UIDevice currentDevice]; BOOL backgroundSupported = NO; if ([device respondsToSelector:@selector(isMultitaskingSupported)]) { backgroundSupported = YES; } if (backgroundSupported) { bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [[UIApplication sharedApplication] endBackgroundTask:bgTask]; }]; /*timer code goes here*/ } 

and my application is running in the background, and I have the above code in the applicationDidEnterBackground method in AppDelegate and getting the following message in gdb

Impossible endBackgroundTask: the background task does not exist with identifier 4, or it may already be completed. Break in UIApplicationEndBackgroundTaskError () for debugging.

I know this question is similar to Cannot endBackgroundTask , but there is no answer or suggestions. any ideas?

+4
ios objective-c
source share
1 answer

Make sure you declare bgTask as follows: UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;

I think you can have bgTask declared as int or NSNumber.

+5
source share

All Articles