kailee kailee - 3 months ago 9
iOS Question

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

The AppDelegate.m file contains

- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIBackgroundTaskIdentifier taskID = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:taskID];
}];
}


I don't know why I have got this message in the gdb


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

Answer

Your code is all wrong. It should be something like this:

UIBackgroundTaskIdentifier taskID = [application beginBackgroundTaskWithExpirationHandler:^{
    // Code to ensure your background processing stops executing
    // so it reaches the call to endBackgroundTask:
}];

// Put the code you want executed in the background here

if (taskID != UIBackgroundTaskInvalid) {
    [[UIApplication sharedApplication] endBackgroundTask:taskID];
}
Comments