-->

iOS的不是典型的背景位置跟踪定时器问题(iOS Not the typical backgroun

2019-07-03 20:04发布

我正在开发跟踪在后台用户的位置一个示例应用程序,但我不想离开这个位置服务始终处于启用状态,但是东西在我的定时器工作不正常。

我的想法是,每x分钟,服务的推移,当它有一个正确的新位置,它被再次释放,现在设置为10秒的时间来进行测试。 (重大LocationChange没有的伎俩,没有足够accurated)

我正在寻找一个很大(的iOS开发中心+的StackOverflow),发现“新”后台定位功能,它允许你去后台后10分钟运行代码,使用beginBackgroundTaskWithExpirationHandler,几个街区,和一个计时器。

我设置背景模式,定位与现在我认为我没有需要处理的背景时(首先我想每15-20秒的位置)结束

该代码工作“精”,但是:

  • 计时器有时火灾,有时不能。
  • 当计时器火灾,它需要至少10分钟,以做到这一点。
  • 在OS一些随机的操作(如输入要搜索的桌面)似乎estimulate计时器射击(不知道这一点,我不知道它是如何可能的,但它是...)

并通过在代码将是另外一个问题。

appdelegates的方法:

// applicationDidEnterBackground

- (void)applicationDidEnterBackground:(UIApplication *)application{

NSLog(@"to background");

UIApplication*    app = [UIApplication sharedApplication];

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app 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.
    _triggertimer = nil;
    [self initTimer];

});
NSLog(@"backgroundTimeRemaining: %.0f", [[UIApplication sharedApplication] backgroundTimeRemaining]);}

// 为inittim

- (void) initTimer{
NSLog(@"InitTimer ");


UIApplication *app = [UIApplication sharedApplication];

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{



_triggertimer = [NSTimer scheduledTimerWithTimeInterval:10.0
                                                     target:self
                                                   selector:@selector(checkUpdates:)
                                                   userInfo:nil
                                                    repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:_triggertimer forMode:NSDefaultRunLoopMode] ;
[[NSRunLoop currentRunLoop] run];

}];}

// checkUpdates

- (void)checkUpdates:(NSTimer *)timer{

NSLog(@"CheckUpdates ");

UIApplication*    app = [UIApplication sharedApplication];

if (nil == _locationManager) _locationManager = [[CLLocationManager alloc] init];

_locationManager.delegate = self;


_locationManager.distanceFilter = 10;
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;

[_locationManager startUpdatingLocation];
[_locationManager startMonitoringSignificantLocationChanges];



double remaining = app.backgroundTimeRemaining;
NSLog(@"Reminaing %f", remaining);}

我想很多的事情,试图解决这个问题,也许我搞砸或错过了什么?你看到了什么? 也许有些概念的错误,我想介绍自己的块,我不域他们还¬¬

顺便说一句,为什么所有我发现代码中包含这与beginBackgroundTaskWithExpirationHandler做任何事情之前?

 bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

我认为这是采取的背景是600秒...但我不知道!

Answer 1:

当您的应用程序中背景的计时器将不再火灾,除非你有“位置”值UIBackgroundModes关键在应用程序的Info.plist设置。

你可以扩展你被允许在后台运行(如果您没有设置“位置”)使用beginBackgroundTaskWithExpirationHandler但始终应与相应的结束通话配对的时间。



Answer 2:

好了,问题是,我打电话beginBackgroundTaskWithExpirationHandler两次,一次在ApplicationDidEnterBackground,另一个里面initTimer ...



文章来源: iOS Not the typical background location tracking timer issue