swift - 有谁知道如何快速解决 BackgroundTask 问题?

标签 swift ios12 ios13 background-task backgroundtaskidentifier

这个问题在这里已经有了答案:





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/

相关文章:

ios - iOS 13 中的条形按钮色调颜色

ios - swift 。如何在 ViewController 以外的单独的 swift 文件中编写 UICollectionView

swift - SwiftUI 是否向后兼容 iOS 12.x 及更早版本?

ios - swift-在ios中使用未声明的类型 'firebase'

ios - GLKView 类的替代解决方案是什么? [在 iOS 12 中已弃用]

iOS 12 特定问题 : Core Data External Storage Binary Data corruption

ios - SwiftUI : Picker does not update correctly when changing datasource

javascript - 防止在 iOS 13/Mobile Safari 上点击并按住文本选择

ios - 我在这里需要一个便利的初始值设定项吗?

ios - 如何从左到右对齐 Collection View 单元格?