有谁知道如何迅速解决BackgroundTask问题?

时间:2019-09-14 06:14:46

标签: swift ios12 ios13 background-task backgroundtaskidentifier

将iPad更新到iOS 13后,我正在应用程序内部使用后台任务,我的应用程序发出了以下消息:

  

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

我使用UIApplicationEndBackgroundTaskError()进行了调试,但没有得到任何结果,并且我已经在iOS 12和其他早期版本上对其进行了测试,效果很好。

1 个答案:

答案 0 :(得分:0)

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

}