Does anyone know how to solve with BackgroundTask issues in swift?

7.6k views Asked by At

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.

1

There are 1 answers

0
oskarko On

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

}