【问题标题】:Can't endBackgroundTask: no background task exists with identifier 1fd57580, or it may have already been endedCan't endBackgroundTask:不存在标识符为 1fd57580 的后台任务,或者它可能已经结束
【发布时间】:2015-10-23 05:24:50
【问题描述】:

AppDelegate.m 文件包含

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIBackgroundTaskIdentifier taskID = [application beginBackgroundTaskWithExpirationHandler:^{
        [application endBackgroundTask:taskID];
    }];
}

我不知道为什么我会在 gdb 中收到此消息

无法结束BackgroundTask:不存在具有标识符的后台任务 1fd57580,或者它可能已经结束。打破 UIApplicationEndBackgroundTaskError() 进行调试。

【问题讨论】:

    标签: ios xcode6


    【解决方案1】:

    你的代码全错了。应该是这样的:

    UIBackgroundTaskIdentifier taskID = [application beginBackgroundTaskWithExpirationHandler:^{
        // Code to ensure your background processing stops executing
        // so it reaches the call to endBackgroundTask:
    }];
    
    // Put the code you want executed in the background here
    
    if (taskID != UIBackgroundTaskInvalid) {
        [[UIApplication sharedApplication] endBackgroundTask:taskID];
    }
    

    【讨论】:

    • 我从网络上看到了一些类似这样的答案:[application endBackgroundTask:taskID]; taskID = UIBackgroundTaskInvalid;我从分析中发现“存储到'taskID'的值永远不会被读取”,所以taskID = UIBackgroundTaskInvalid;是可选的???
    • 我不太确定你在评论中要问什么,但它让我想起了一个小变化。请参阅我的更新答案。
    【解决方案2】:

    如果您在后台使用位置更新,请在获取用户位置授权时添加以下代码。这是因为 Apple 从 iOS 9 开始将默认值 allowsBackgroundLocationUpdates 更改为 NO

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
        locationManager.allowsBackgroundLocationUpdates = YES;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-11
      • 2014-07-15
      • 2020-05-26
      • 2020-02-27
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多