【问题标题】:Does anyone know how to solve with BackgroundTask issues in swift? [duplicate]有谁知道如何快速解决 BackgroundTask 问题? [复制]
【发布时间】:2020-01-15 21:08:34
【问题描述】:

在我的 iPad 更新到 iOS 13 后,我在我的应用程序中使用后台任务,我的应用程序发出这个:

无法结束 BackgroundTask:不存在标识符 > 13 (0xd) 的后台任务,或者它可能已经结束。中断 UIApplicationEndBackgroundTaskError() 进行调试。

我使用 UIApplicationEndBackgroundTaskError() 进行了调试,但没有得到任何结果,我已经在 iOS 12 和其他以前的版本上测试了它,它运行良好。

【问题讨论】:

  • 我在 iOS 13 下看到了同样的错误,所以不仅仅是你。可能是 iOS 13 的错误,但由于优先级更高,我没有花任何时间研究这个错误。
  • @rmaddy 谢谢兄弟,但现在这是我的应用程序的优先事项。
  • @ShreeramBhat 谢谢兄弟 BGTaskSchedular 是一个不同的话题。
  • @ShreeramBhat 我的应用在进入后台时无法通知。

标签: swift ios12 ios13 background-task backgroundtaskidentifier


【解决方案1】:

你需要设置 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;
});

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2019-05-22
    相关资源
    最近更新 更多