title: iOS Not the typical background location tracking timer issue
body: <p>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.</p>
  
  <p>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)</p>
  
  <p>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.</p>
  
  <p>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)</p>
  
  <p>the code is working "fine" but:</p>
  
  <ul>
  <li>The timer sometimes fires, sometimes does not.</li>
  <li>When the timer fires, it takes a minimum of 10 minutes to do it.</li>
  <li>Some random actions in the OS (like entering to search desktop) appears to estimulate the timer to fire (not sure of this, i don't realize how it is possible, but there it is...)</li>
  </ul>
  
  <p>And by over the code will be another qüestion.</p>
  
  <p>appdelegates's methods:</p>
  
  <p>//<strong>applicationDidEnterBackground</strong></p>
  
  <pre><code>- (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]);}
  </code></pre>
  
  <p>//<strong>initTimer</strong></p>
  
  <pre><code>- (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];
  
  }];}
  </code></pre>
  
  <p>//<strong>checkUpdates</strong></p>
  
  <pre><code>- (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);}
  </code></pre>
  
  <p>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 ¬¬</p>
  
  <p>By the way,why all the codes i've found contains this before doing anything with <strong>beginBackgroundTaskWithExpirationHandler</strong>?</p>
  
  <pre><code> bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
      [app endBackgroundTask:bgTask];
      bgTask = UIBackgroundTaskInvalid;
  }];
  </code></pre>
  
  <p>I thought that this is for taking that 600 seconds of background... but i'm not sure!</p>