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

I am using a background task to start a timer in the background to update a user's location. It is declared as:

UIBackgroundTaskIdentifier bgTask; 

in the header file and initialized as:

 bgTask = UIBackgroundTaskInvalid; 

But still I get this message in gdb:

Impossible endBackgroundTask: there is no background task with id 23dc or it may already be completed. Break in UIApplicationEndBackgroundTaskError () for debugging.

Why? And how can I solve this?

+10
objective-c ios7 xcode5
source share
5 answers

You need to set bgTask = UIBackgroundTaskInvalid

in two moments

  • In the expiration handler.
  • After completing your task.

I believe that you are missing one of these two points, and that is why you are getting this error.

See Apple sample code example:

 - (void)applicationDidEnterBackground:(UIApplication *)application { bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{ // Clean up any unfinished task business by marking where you // stopped or ending the task outright. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task, preferably in chunks. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); } 

link: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

+4
source share

Are you using location update in the background?

If so, add the code below when receiving location authorization from the user - Apple has changed the default value for allowsBackgroundLocationUpdates to NO from iOS 9 onwards.

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) { locationManager.allowsBackgroundLocationUpdates = YES; } 

Make sure you activate Location Updates for the background in your Signing & Enhancing project. Capabilities. Otherwise, it will crash.

+3
source share

In Xcode, switch to the breakpoint navigator (View> Navigators> Show Breakpoint Navigator), then click the + button in the lower left corner and select Add a Symbolic Breakpoint and enter UIApplicationEndBackgroundTaskError as the character .

+2
source share

I am losing many days looking for the piece of code or infrastructure that caused this warning in the debug console. Cannot complete BackgroundTask: there is no background task with identifier 2 (0x2), or it may already be completed. Break in UIApplicationEndBackgroundTaskError () for debugging.

Finally, I created an empty Single View App project. Only the code generated by Xcode, I run the application on the simulator, put it in the background and see the same warning. So I can say that this is a problem of iOS 13. I hope that Apple fixes it quickly, because in Crashlytics I found some failure in my application caused by this.

0
source share

So bgTask works as follows:

 bgTask = [application beginBackgroundTaskWithName:kBGTaskName expirationHandler:^{} 

Do your work inside the block.
You will also get a Can not endBackgroundtask error if you place a breakpoint inside a block, do some work and let it run, as they say in 10 seconds or so. Instead of a breakpoint, set NSLog and check. It should work fine.

-one
source share

All Articles