iOS 不是典型的后台位置跟踪计时器问题 [英] iOS Not the typical background location tracking timer issue
问题描述
我正在开发一个在后台跟踪用户位置的示例应用程序,但我不想让定位服务始终处于启用状态,但我的计时器中的某些内容无法正常运行.
i'm developing a sample app that tracks the user's position in background, but i don't want to leave the location service always enabled, but something in my timer does not behave properly.
我的想法是,每隔 x 分钟,服务就会继续,当它有一个正确的新位置时,它会再次发布,现在设置为 10 秒,仅用于测试.(显着的 LocationChange 不是诀窍,不够准确)
My idea was that every x minutes, the service goes on, and when it have a correct new location it is released again, now is set to 10 seconds just for testing. (Significant LocationChange did not the trick, not accurated enough)
我搜索了很多(iOS 开发中心 + StackOverflow)并找到了新的"后台定位功能,它允许您在进入后台后 10 分钟内运行代码,使用 beginBackgroundTaskWithExpirationHandler、几个块和一个计时器.
I was searching a lot (iOS Dev center + StackOverflow) and found the "new" background location features, that allows you to run code over 10 minutes after going to background, using beginBackgroundTaskWithExpirationHandler, a few blocks, and a timer.
我将后台模式设置为位置,现在我认为我不需要处理后台时间的结束(首先我想每 15-20 秒获取一次位置)
I set the background mode to Location and by now i think i don't need to handle the end of the background time (first i want to get a location every 15-20 seconds)
代码运行良好,但是:
- 计时器有时会触发,有时不会.
- 当计时器触发时,至少需要 10 分钟.
- 操作系统中的一些随机操作(例如进入搜索桌面)似乎会估计触发计时器(不确定这一点,我不知道这是怎么可能的,但它确实存在......)
通过代码将是另一个问题.
And by over the code will be another qüestion.
appdelegates 的方法:
appdelegates's methods:
//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]);}
//initTimer
- (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];
}];}
//检查更新
- (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);}
我尝试了很多方法来解决这个问题,也许我搞砸了或错过了什么……你看到了什么?也许是一些概念错误,我正在尝试向这些块介绍自己,但我还没有对它们进行域处理 ¬¬
I tried lots of thing to try to fix this and maybe i messed or missed something... What do you see? maybe some concept errors, i'm trying to introduce myself to the blocks and I don't domain them yet ¬¬
顺便说一句,为什么在使用 beginBackgroundTaskWithExpirationHandler 做任何事情之前我发现的所有代码都包含这个?
By the way,why all the codes i've found contains this before doing anything with beginBackgroundTaskWithExpirationHandler?
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
我以为这是为了拍摄那 600 秒的背景……但我不确定!
I thought that this is for taking that 600 seconds of background... but i'm not sure!
推荐答案
好的,问题是我调用了两次 beginBackgroundTaskWithExpirationHandler,一次在 ApplicationDidEnterBackground 中,另一次在 initTimer>...
Ok, the problem was that i was calling beginBackgroundTaskWithExpirationHandler twice, one in ApplicationDidEnterBackground and another one inside initTimer...
这篇关于iOS 不是典型的后台位置跟踪计时器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!