【问题标题】:Can't endBackgroundTask: no background task exists with identifier 4, or it may have already been endedCan't endBackgroundTask:不存在标识符为 4 的后台任务,或者它可能已经结束
【发布时间】:2013-07-11 20:11:11
【问题描述】:

使用XCode 4.5.2在后台实现定时器如下

UIDevice *device = [UIDevice currentDevice];
    BOOL backgroundSupported = NO;
    if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {
        backgroundSupported = YES;
    }

    if (backgroundSupported) 
    {
        bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
            [[UIApplication sharedApplication] endBackgroundTask:bgTask];
        }];

      /*timer code goes here*/
    }

我的应用程序正在后台运行,我在 AppDelegate 的 applicationDidEnterBackground 方法中有上述代码,并在 gdb 中收到以下消息

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

我知道这个问题类似于Can't endBackgroundTask,但那里没有答案或建议。有什么想法吗?

【问题讨论】:

  • bgtask的类型是什么?

标签: ios objective-c


【解决方案1】:

确保您像这样声明bgTaskUIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;

我认为您可能将 bgTask 声明为 int 或 NSNumber。

【讨论】:

  • 当然,我会试试这个:) 谢谢。
  • Xcode 在尝试将 bgTask 设置为 nil 时给出编译器警告。
  • 将我的答案更新为适当的默认值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-15
  • 2020-05-26
  • 2020-02-27
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多