title: Update Location in Background every n minutes forever in Swift 2.0
body: <p>With the code below in AppDelegate.swift I can get location updates every 30 seconds. However, after 180 seconds the background execution is kicked by backgroundTimeRemaining. <br>The plist has the "location"tag in UIBackgroundModes
  <br>I have two questions:</p>
  
  <ol>
  <li>Why the backgroundTimeRemaining is not resetting?</li>
  <li>If I do startUpdatingLocation() and then stopUpdatingLocation() to save battery then I will not get location updated ever again.</li>
  </ol>
  
  <p></p>
  
  <pre><code>import UIKit
  import CoreLocation
  @UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
  
      var window: UIWindow?
      let locationManager = CLLocationManager()
      var pos = 0
      var LatitudeGPS = NSString()
      var LongitudeGPS = NSString()
      var speedGPS = NSString()
      var Course = NSString()
      var Altitude = NSString()
      var bgtimer = NSTimer()
  
      func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
          // Override point for customization after application launch.
          return true
      }
  
      var backgroundUpdateTask: UIBackgroundTaskIdentifier!
  
      func beginBackgroundUpdateTask() {
          self.backgroundUpdateTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
          self.endBackgroundUpdateTask()
          })
      }
  
      func endBackgroundUpdateTask() {
          UIApplication.sharedApplication().endBackgroundTask(self.backgroundUpdateTask)
          self.backgroundUpdateTask = UIBackgroundTaskInvalid
      }
  
      func doBackgroundTask() {
  
          dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
              self.beginBackgroundUpdateTask()
  
              // Do something
              self.StartupdateLocation()
  
              self.bgtimer = NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: "bgtimer:", userInfo: nil, repeats: true)
              NSRunLoop.currentRunLoop().addTimer(self.bgtimer, forMode: NSDefaultRunLoopMode)
              NSRunLoop.currentRunLoop().run()
  
              // End the background task.
              self.endBackgroundUpdateTask()
          })
      }
  
      func bgtimer(timer:NSTimer!){
  
          print("Fired from Background ************************************")
  
          updateLocation()
  
          print("Position Report: \(pos)")
      }
  
      func applicationDidEnterBackground(application: UIApplication) {
  
          self.doBackgroundTask()
      }
  
      func StartupdateLocation() {
          locationManager.delegate = self
          locationManager.startUpdatingLocation()
          locationManager.desiredAccuracy = kCLLocationAccuracyBest
          locationManager.distanceFilter = kCLDistanceFilterNone
          locationManager.requestWhenInUseAuthorization()
          locationManager.allowsBackgroundLocationUpdates = true
          locationManager.pausesLocationUpdatesAutomatically = false
      }
  
      func updateLocation() {
          pos++
  
          //locationManager.startUpdatingLocation()
          //locationManager.stopUpdatingLocation()
  
          print("Latitude: \(LatitudeGPS)")
          print("Longitude: \(LongitudeGPS)")
          print("Speed: \(speedGPS)")
          print("Heading: \(Course)")
          print("Altitude BG: \(Altitude)")
          print(UIApplication.sharedApplication().backgroundTimeRemaining)
      }
  
      func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  
          LatitudeGPS = String(format: "%.10f", manager.location!.coordinate.latitude)
          LongitudeGPS = String(format: "%.10f", manager.location!.coordinate.longitude)
          speedGPS = String(format: "%.3f", manager.location!.speed)
          Altitude = String(format: "%.3f", manager.location!.altitude)
          Course = String(format: "%.3f", manager.location!.course)
      }
  }
  </code></pre>
  
  <p>EDIT ***
  I solved the problem after I initialized the updateLocation in the ViewController.</p>