LocationDataManager.swift
Purpose
The LocationDataManager.swift class handles location tracking using CoreLocation and coordinates with the FileHandler to attach location metadata to sensor data collected by the HealthyWear app.
Key Responsibilities
Manages continuous location updates with
CLLocationManager.Provides the last known location to sensor data when saving.
Verifies location permission status and responds to system callbacks.
Designed as a singleton to allow shared access across modules.
Singleton Setup
static let shared = LocationDataManager()
Provides global access to a single instance across the iOS app.
Properties
private var locationManager = CLLocationManager()
private var lastKnownLocation: CLLocation?
locationManager: CoreLocation’s central controller.lastKnownLocation: Stores the most recent location data.
Initialization
override init() {
super.init()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
Requests permission and sets up the delegate on initialization.
Start Location Updates
private func startLocationUpdates()
Uses
DispatchQueue.main.asyncto ensure updates run on the main thread.Sets
desiredAccuracytokCLLocationAccuracyHundredMetersfor balance of precision and battery.TODO: Improve by throttling updates (e.g., every 50 seconds).
Saving Sensor Data with Location
func fetchLocationAndSaveData(dataType: String, dataBuffer: [String], completion: @escaping (Bool) -> Void)
Ensures a valid location exists.
Passes location coordinates and buffer to
FileHandler.shared.saveDataToFile.
CLLocationManagerDelegate Methods
didUpdateLocations
Updates the lastKnownLocation with the most recent GPS result.
didFailWithError
Handles failure and prints debugging message.
Utility Method
func checkLocationServicesAndNotify()
Verifies if location services are enabled.
If not, prints instructions to enable them in device settings.
Integration
Used whenever sensor data must be saved with geolocation context. This includes:
Smart sock recordings
Smart band sensor streams
Motion sensors