Search code examples
ioscore-locationbattery

core location distance filter battery life


Does changing the distanceFilter value have any effect on battery life? If so what is this effect and why should it have any effect?


Solution

  • The distanceFilter property has no or extremely minimal affect on battery life. All it does restrict calls to the delegate to every n meters that it moves. If conserving battery life is what you want to do with gps then I recommend only turning on the gps and taking a reading every n seconds, then turning it off again until the next reading. I would use a timer. If you want this to also work in the background take the following steps:

    1. In your apps plist file add the "app registers for location updates" to the Required background modes key.

    2. In your applicationDidEnterBackground method do something like this

      UIBackgroundTaskIdentifier __block bgTask;
      
      UIApplication *application = [ UIApplication sharedApplication];
      
      bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
          //Prevent from going inactive by starting location update
          [[ ApplicationContext sharedInstance].coreLocationController startUpdatingLocation ];
          [application endBackgroundTask:bgTask];
           bgTask = UIBackgroundTaskInvalid;
       }];
      
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         while (YES) {
            if ( [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive ) {
              break;
          } else {
           //DO something in the background
          }
      
      }
      });
      

      Where you are starting the location manager just before the app would expire to ensure that it continues its active state in the background. This will allow the app to always run in the background so you can intermittently check the location by turning gps on/off