title: Getting user location every n minutes after app goes to background
body: <p>I am trying to implement the suggestions given in <a href="https://stackoverflow.com/questions/6347503/how-do-i-get-a-background-location-update-every-n-minutes-in-my-ios-application">this post</a>.</p>
  
  <p>Unfortunately the steps are not clear to me. I tried implementing those suggestions, but the backgroundTimeRemaining continues to decrease even after I start and stop locationServices.  This is how I developed it:</p>
  
  <pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application {
  
      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.
          self.timer = nil;
          [self initTimer];
  
      });
  }
  </code></pre>
  
  <p>initTimer:</p>
  
  <pre><code>- (void)initTimer {
  
      // Create the location manager if this object does not
      // already have one.
      if (nil == self.locationManager)
          self.locationManager = [[CLLocationManager alloc] init];
  
      self.locationManager.delegate = self;
      [self.locationManager startMonitoringSignificantLocationChanges];
  
      if (self.timer == nil) {
          self.timer = [NSTimer scheduledTimerWithTimeInterval:0.3
                                                target:self
                                              selector:@selector(checkUpdates:)
                                              userInfo:nil
                                               repeats:YES];
      }
  }
  </code></pre>
  
  <p>checkUpdates:</p>
  
  <pre><code>- (void)checkUpdates:(NSTimer *)timer{
      UIApplication*    app = [UIApplication sharedApplication];
      double remaining = app.backgroundTimeRemaining;
      if(remaining &lt; 580.0) {
          [self.locationManager startUpdatingLocation]; 
          [self.locationManager stopUpdatingLocation]; 
          [self.locationManager startMonitoringSignificantLocationChanges];
      }
      DbgLog(@"Reminaing %f", app.backgroundTimeRemaining);
  }
  </code></pre>
  
  <p>Does anyone have a suggestion on what might be wrong in my code? Both initTimer and checkUpdates are being called, but only during for the background execution time (+- 10 Mins). I want the app to update the location every n minutes "forever".</p>
  
  <p>My app's UIBackgroundModes is set to location.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I am now resetting the timer on didUpdateToLocation and didFailWithError. But still the backgroundTimeRemaining keeps decreasing:</p>
  
  <pre><code>- (void)locationManager:(CLLocationManager *)manager 
  didUpdateToLocation:(CLLocation *)newLocation 
         fromLocation:(CLLocation *)oldLocation {
  
  NSLog(@"Did Update Location = %f / %f", [newLocation coordinate].latitude, [newLocation coordinate].longitude);
  
  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.
  
      [self initTimer];
  
  });
  }
  
  - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  
  [self.locationManager stopUpdatingLocation]; 
  UIApplication*    app = [UIApplication sharedApplication];
  
  bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
      [app endBackgroundTask:bgTask];
      bgTask = UIBackgroundTaskInvalid;
  }];
  
  // Start the long-running task and return immediately.
  [self initTimer];
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  
      // Do the work associated with the task.
  
  });
  
  }
  </code></pre>
  
  <p>I am also invalidating the timer:</p>
  
  <pre><code>- (void)checkUpdates:(NSTimer *)timer{
  UIApplication*    app = [UIApplication sharedApplication];
  double remaining = app.backgroundTimeRemaining;
  if(remaining &lt; 580.0 &amp;&amp; remaining &gt; 570.0) {
      [self.timer invalidate];
      self.timer = nil;
      [self.locationManager startUpdatingLocation]; 
      [self.locationManager stopUpdatingLocation]; 
  }
  DbgLog(@"*************************Checking for updates!!!!!!!!!!! Reminaing %f", app.backgroundTimeRemaining);
  }
  </code></pre>