5

I am using background task inside my app after updating my iPad to iOS 13 my application issuing this:

Can't end BackgroundTask: no background task exists with identifier > 13 (0xd), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.

I debugged with UIApplicationEndBackgroundTaskError() but didn't get any result and I have tested my on iOS 12 and other previous versions it worked perfectly.

Ali Qaderi
  • 471
  • 7
  • 16
  • Refer this https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler – Shreeram Bhat Sep 14 '19 at 11:37
  • 2
    I'm seeing the same error under iOS 13 so it's not just you. Might be an iOS 13 bug but I haven't spent any time looking into this one due to higher priorities. – rmaddy Sep 14 '19 at 15:45
  • @rmaddy thanks bro but right now this is a priority for my app. – Ali Qaderi Sep 15 '19 at 03:48
  • @ShreeramBhat thanks bro BGTaskSchedular is a different topic. – Ali Qaderi Sep 15 '19 at 03:49
  • @ShreeramBhat My app can't notify when it goes to background. – Ali Qaderi Sep 15 '19 at 03:49
  • @rmaddy does this actually close your app while in the background on user's devices? according to my crash service my users' apps are getting killed by the thousands each day starting on October 15 when iOS 13.1.3 was released (though no official crash reports accompany - just that app did not terminate cleanly) – SAHM Nov 01 '19 at 14:16
  • I answered this here. I hope it helps! https://stackoverflow.com/a/58763049/1890317 – uplearned.com Nov 08 '19 at 08:45
  • @uplearnedu.com thank you. – Ali Qaderi Nov 10 '19 at 06:12

1 Answers1

0

You need to set bgTask = UIBackgroundTaskInvalid

in two moments

In the expiration handler. After finishing your task.

I believe you are missing any of those two moments and that is why you are getting that error.

See apple example code:

- (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;
});

}

oskarko
  • 3,382
  • 1
  • 26
  • 26