Oksana
Oksana

Reputation: 13952

Many CLLocationManager instance

If I init additional CLLocaitonManager instance that track user location, is It will increase the load? Or Should I using one CLLocaitonManager instance between classes?

Upvotes: 6

Views: 3366

Answers (3)

spnkr
spnkr

Reputation: 1372

It's ok to have multiple instances of CLLocationManager in your code. For example, one CLLocationManager could be set up to monitor significant location changes, and another could be set to monitor geofencing. This can be useful if you want to have different distance filter and/or accuracy parameters.

Monitoring visits via startMonitoringVisits will enable visit monitoring for all CLLocationManager instances in your app. Other methods don't do this (as of iOS 17).

If you want to receive location updates in the background, I wouldn't tie it to a view controller. Since there may not be any view controllers when the location change notification is fired.

As far as increasing the load on the system, it doesn't make much difference how many location managers you have. What matters is the parameters you set. If you set accuracy to kCLLocationAccuracyBestForNavigation, this takes more battery. See also:

  • activityType
  • pausesLocationUpdatesAutomatically
  • distanceFilter
  • allowsBackgroundLocationUpdates
  • showsBackgroundLocationIndicator
  • monitoredRegions

Upvotes: 1

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73678

Creating too many CLLocationManager or increasing the update intervals of the Core location services severely drains battery. So creating too many instances is not advised. Dont see a need for this.

A good practice is to init one CLLocationManager in a viewController. If moving to another viewController, then stopUpdates on the current CLLocationManager & create a new manager in the new viewController. This is one pattern.

Another pattern is to create a CLLocationManager in app delegate & make it available throughout your app. This is like a global variable. But generally avoid global declaration of this variable because it continuously consumes your battery life.

So basically if all your classes are part of only one viewController then create only one CLLocationManager & share the location updates. If not then create one for each viewController.

Upvotes: 7

Sudesh Kumar
Sudesh Kumar

Reputation: 569

Location services require the presence of specific hardware on the given device. So thats why ,its doesn't matter how many instance of CLLocationManager are created. Generally avoid global declaration of this variable because it continuously consumes your battery life. Another wise stop and start each time when ever you need to finding user location.

Upvotes: 0

Related Questions