【问题标题】:NSRunLoop for background task validNSRunLoop 用于后台任务有效
【发布时间】:2015-12-22 06:01:01
【问题描述】:

我试图让我的 iOS 应用程序在后台永远运行,(或关于位置的位置采样和诊断)我找到了这段代码 -

 [self.locationManager stopUpdatingLocation];
 self.timer = [NSTimer scheduledTimerWithTimeInterval:self.currentTimerTime target:self selector:@selector(checkLocation) userInfo:nil repeats:NO];
 [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
 [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil];

这很好用,但我觉得苹果不喜欢这样,这是最佳做法吗?

【问题讨论】:

    标签: ios location nsrunloop background-task


    【解决方案1】:

    您可能已经发现,虽然这在开发中有效,但在生产中却不适用。应用程序获得beginBackgroundTaskWithExpirationHandler 获得有限的执行时间,在开发中不受限制,但在已发布的应用程序中通常为 60 秒,此时将调用到期处理程序(您未在示例代码中分配),并且您的应用程序是预期的使用beginBackgroundTaskWithExpirationHandler 调用返回的ID(您没有捕获)结束后台任务。否则将导致您的应用程序被终止。有关更多信息,请参阅此相关问题/答案。

    objective c - Proper use of beginBackgroundTaskWithExpirationHandler

    【讨论】:

      猜你喜欢
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多