这个问题在这里已经有了答案:
Can't endBackgroundTask: no background task exists with identifier, or it may have already been ended
(9 个回答)
1年前关闭。
将 iPad 更新到 iOS 13 后,我在应用程序中使用后台任务,我的应用程序发出以下命令:
Can't end BackgroundTask: no background task exists with identifier > 13 (0xd), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
我使用 UIApplicationEndBackgroundTaskError() 进行了调试,但没有得到任何结果,并且我已经在 iOS 12 和其他以前的版本上测试了它运行良好。
最佳答案
你需要设置 bgTask = UIBackgroundTaskInvalid
在两个瞬间
在到期处理程序中。
完成你的任务后。
我相信您错过了这两个时刻中的任何一个,这就是您收到该错误的原因。
见苹果示例代码:
- (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;
});
}
关于swift - 有谁知道如何快速解决 BackgroundTask 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57932962/