objective-c Can't endBackgroundTask: 不存在带有标识符的后台任务,或者它可能已经结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23882495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't endBackgroundTask: no background task exists with identifier, or it may have already been ended
提问by raj
I am using background task to run the timer in the background to update the user's location. It's declared as:
我正在使用后台任务在后台运行计时器以更新用户的位置。它被声明为:
UIBackgroundTaskIdentifier bgTask;
in the header file, and initialized as:
在头文件中,并初始化为:
bgTask = UIBackgroundTaskInvalid;
But still, I am getting this message in the gdb:
但是,我仍然在 gdb 中收到此消息:
Can't endBackgroundTask: no background task exists with identifier 23dc, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
Can't endBackgroundTask: 不存在标识符为 23dc 的后台任务,或者它可能已经结束。中断 UIApplicationEndBackgroundTaskError() 进行调试。
Why? And how can I solve this?
为什么?我该如何解决这个问题?
回答by Francesco Ceravolo
I lose many days looking for the piece of code or framework that was causing this warning in the debug console Can't end BackgroundTask: no background task exists with identifier 2 (0x2), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
我花了很多天时间在调试控制台中寻找导致此警告的代码或框架无法结束 BackgroundTask:不存在标识符为 2 (0x2) 的后台任务,或者它可能已经结束。中断 UIApplicationEndBackgroundTaskError() 进行调试。
Finally I've created an empty project Single View App. Only code generated by Xcode, I run the app on simulator, put it in background and I see the same warning. So I can say it's an iOS 13 issue. I hope Apple will fix it quickly because in Crashlytics I found some crash in my app caused by it.
最后我创建了一个空项目Single View App。仅由 Xcode 生成的代码,我在模拟器上运行该应用程序,将其置于后台并看到相同的警告。所以我可以说这是一个 iOS 13 问题。我希望 Apple 能尽快修复它,因为在 Crashlytics 中,我发现我的应用程序出现了一些由它引起的崩溃。
回答by Saleh Sultan
In Xcode, switch to the breakpoint navigator (View > Navigators > Show Breakpoint Navigator) then push the + button in the bottom left and select Add Symbolic Breakpoint and enter “UIApplicationEndBackgroundTaskError” as the symbol.
在 Xcode 中,切换到断点导航器(View > Navigators > Show Breakpoint Navigator)然后按下左下角的 + 按钮并选择 Add Symbolic Breakpoint 并输入“UIApplicationEndBackgroundTaskError”作为符号。
回答by Julian Osorio
You need to set bgTask = UIBackgroundTaskInvalid
你需要设置 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;
});
}
link: https://developer.apple.com/documentation/backgroundtasks/bgtask?language=objc
链接:https: //developer.apple.com/documentation/backgroundtasks/bgtask?language=objc
回答by Anand3777
Are you using location update in the background?
您是否在后台使用位置更新?
If yes, add the below code at the time of getting location authorization from the user - Apple changed the default for allowsBackgroundLocationUpdatesto NOfrom iOS 9 onwards.
如果是,在从用户获取位置信息授权的时候添加下面的代码-苹果改变了默认为allowsBackgroundLocationUpdates以NO从iOS的9起。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
locationManager.allowsBackgroundLocationUpdates = YES;
}
Please make sure that you activated the 'Location updates' for background modes under your project 'Signing & Capabilities'. Otherwise it will crash.
请确保您在您的项目“签名和功能”下为后台模式激活了“位置更新”。否则会崩溃。
回答by uplearnedu.com
Using Swift 5 - IOS 13.2.2 - Xcode 11.2 beta 2 (11B44)I managed to get rid of this error by
使用 Swift 5 - IOS 13.2.2 - Xcode 11.2 beta 2 (11B44)我设法摆脱了这个错误
enabling Push notifications in the Capabilities of the app
Import the UserNotifications framework
then add this code to
在应用程序的功能中启用推送通知
导入 UserNotifications 框架
然后将此代码添加到
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// If granted comes true you can enabled features based on authorization.
guard granted else { return }
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
回答by Hans Terje Bakke
Reproduced, here is my entire app:
转载,这是我的整个应用程序:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
return true
}
}
And when I take it to background I get the same message. No storyboard or any other noise.
当我把它带到后台时,我得到了同样的信息。没有故事板或任何其他噪音。
回答by Rob
Regarding the “Can't end BackgroundTask: no background task exists with identifier ...” message, as others have said, this can be a red herring, unrelated to what you're doing.
关于“无法结束BackgroundTask:不存在带有标识符的后台任务......”消息,正如其他人所说,这可能是一个红鲱鱼,与您正在做的事情无关。
Regarding your goal of trying to periodically retrieve the user's location, rather than trying to keep your app running in the background with a timer, you should instead avail yourself of various services for updating the user location in the background, such as the visits location serviceor the significant change location service. With these services, the OS can wake your app when there is a change in the user's location.
关于您尝试定期检索用户位置的目标,而不是尝试使用计时器让您的应用程序在后台运行,您应该利用各种服务在后台更新用户位置,例如访问位置服务或显着改变位置服务。通过这些服务,操作系统可以在用户位置发生变化时唤醒您的应用。
回答by Manivel Nagarajan
I had the same issue and figured out the cause and solution!
我遇到了同样的问题,并找出了原因和解决方案!
Apple has fixed this issue in iOS 13.4 GM though I have workaround for the earlier versions.
Apple 已经在 iOS 13.4 GM 中修复了这个问题,尽管我有针对早期版本的解决方法。
While you should embrace using scenes when your app is run under iOS 13 and later, you can fully opt-out while you still support iOS 12 or earlier.
当您的应用在 iOS 13 及更高版本下运行时,您应该接受使用场景,但您可以在仍然支持 iOS 12 或更早版本的情况下完全选择退出。
- First, completely remove the “
Application Scene Manifest” entry from Info.plist. - If there is a scene delegate class, remove it.
- If there are any scene related methods in your app delegate, remove those methods.
- If missing, add the property
var window: UIWindow?to your app delegate. - Your app should now only use the app delegate and under iOS 13 it should have the same life cycle as iOS 12.
- 首先,
Application Scene Manifest从 Info.plist 中完全删除“ ”条目。 - 如果存在场景委托类,请将其删除。
- 如果您的应用程序委托中有任何与场景相关的方法,请删除这些方法。
- 如果丢失,请将该属性添加
var window: UIWindow?到您的应用程序委托。 - 您的应用现在应该只使用应用委托,并且在 iOS 13 下它应该具有与 iOS 12 相同的生命周期。
Happy coding!
快乐编码!
回答by abanet
I think there is a bug working with xcode 11.2 and the swiftUI framework because I always get the
我认为使用 xcode 11.2 和 swiftUI 框架存在一个错误,因为我总是得到
"Can't end BackgroundTask: no background task exists with identifier..."
“无法结束BackgroundTask:不存在带有标识符的后台任务......”
message even when my app is not working with background tasks! The good news is that this only stops my app when executing from xcode; use the 'continue program execution' button and the app will be running again.
即使我的应用程序没有处理后台任务,也会发送消息!好消息是,这只会在从 xcode 执行时停止我的应用程序;使用“继续程序执行”按钮,应用程序将再次运行。

