Reputation: 117
I am building a paragliding app for watchOS, that can display and log values like altitude, speed, glide ratio and so on.
So far I built the UI, integrated some settings and I am receiving pressure sensor data and gps location. To save battery life I included a "stop button" that triggers the locationManager.stopUpdatingLocation and CMAltimeter.stopRelativeAltitudeUpdates functions. But in the console I can see still location and altitude updates coming in.
I uploaded the code to https://github.com/LukeCrouch/iAlti/tree/main/iAlti%20WatchKit%20Extension . The interesting file is probably the ControlsView, here I declare and call the relevant functions.
func stopLocation() {
self.locationManager.stopUpdatingLocation()
}
I appreciate every bit of help I can get, this is my first time writing an App! :)
Cheers Luke
Upvotes: 1
Views: 457
Reputation: 535925
The problem is this line:
@ObservedObject var locationManager = LocationManager()
Every time the ControlsView is regenerated, that line runs afresh. So the LocationManager is a different LocationManager. So you are telling a LocationManager's CLLocationManager to stop updating, but this is a different CLLocationManager from the one that started.
Instead, make this a singleton environment object so that your app has just one LocationManager with just one CLLocationManager. That way, each time you talk to it, you are talking to the same one.
Upvotes: 1