iOS的不是典型的背景定位跟踪计时器问题 [英] iOS Not the typical background location tracking timer issue

查看:187
本文介绍了iOS的不是典型的背景定位跟踪计时器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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没有的伎俩,没有足够的accurated)

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开发中心+计算器),发现新后台定位功能,可让您将背景下,使用beginBackgroundTaskWithExpirationHandler,几个街区后,在运行10分钟code和一个计时器。

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)

在code正在精,而是:

the code is working "fine" but:


  • 定时器有时火灾,有时不会。

  • 当计时器火灾,它需要至少10分钟做到这一点。

  • 在OS一些随机的动作(如进入搜索桌面)似乎estimulate计时器射击(不知道这一点,我不知道它是如何可能的,但它是...)

和通过在code将是另外一个问题。

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];

}];}

// 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);}

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

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 ¬¬

顺便说一句,为什么所有的codeS我找到包含此与做任何事情之前的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆