Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't endBackgroundTask: no background task exists with identifier 4, or it may have already been ended

Implemented the timer in the background as follows and using the 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 app is running in background and I have the above code in applicationDidEnterBackground method in AppDelegate and getting the following message in gdb

Can't endBackgroundTask: no background task exists with identifier 4, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.

I know this question is similar to Can't endBackgroundTask but no answer or suggestions there. any ideas ?

like image 237
Sharme Avatar asked Jul 13 '13 10:07

Sharme


1 Answers

Make sure you are declaring bgTask like so: UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;

I think you may have bgTask declared as an int or NSNumber.

like image 109
Stunner Avatar answered Nov 14 '22 16:11

Stunner