Per usual the Apple documentation is sparse at best and while I appreciate the demo example there are some gaps there too. So the following issues/questions:
- The video and liveUpdatesSample app talk about activating the background activity session which supposedly allows your code to operate in the app is terminated by the user. However, no practical example is provided to show the path to do this successfully. I presume that the code in the startedUpdates would continue to execute if started and the background session is turned on but that is not what happens. Nothing continues or resumes when you relaunch the app.
- The liveUpdatesSample app in the simulator always thinks that the device is not stationary even though I just have Apple set for location
- in the liveUpdateSamples app it's also not clear if/when the background activity flag should be set. I'm guessing it's just a value to be picked up by the AppDelegate when that runs but it does not seem to control any behavior other than to be just a saved boolean.
I would be very helpful to have an example that makes it clearer when and how to ensure and validate that processing is happening in the background when the app is running or terminated.
I would appreciate any insights or guidance on how to ensure my code is running in the background.
Here is where I start the updates:
@MainActor class LocationsHandler: ObservableObject {
let logger = Logger(subsystem: "com.***.yyy", category: "LocationsHandler")
static let shared = LocationsHandler() // Create a single, shared instance of the object.
private let manager: CLLocationManager
private var background: CLBackgroundActivitySession?
@Published var lastLocation = CLLocation()
@Published var count = 0
@Published var isStationary = false
@Published
var updatesStarted: Bool = UserDefaults.standard.bool(forKey: "liveUpdatesStarted") {
didSet { UserDefaults.standard.set(updatesStarted, forKey: "liveUpdatesStarted") }
}
@Published
var backgroundActivity: Bool = UserDefaults.standard.bool(forKey: "BGActivitySessionStarted") {
didSet {
backgroundActivity ? self.background = CLBackgroundActivitySession() : self.background?.invalidate()
UserDefaults.standard.set(backgroundActivity, forKey: "BGActivitySessionStarted")
print("** background actvity changed to: \(backgroundActivity)")
}
}
private init() {
self.manager = CLLocationManager() // Creating a location manager instance is safe to call here in `MainActor`.
}
func startLocationUpdates() {
if self.manager.authorizationStatus == .notDetermined {
self.manager.requestWhenInUseAuthorization()
}
self.logger.info("** Starting location updates")
Task() {
do {
self.updatesStarted = true
let updates = CLLocationUpdate.liveUpdates()
var locationUpdateCounter = 0
for try await update in updates {
/// If updates are not started the break out of the loop
if !self.updatesStarted { break }
/// If the location is not nill then we can process the update
if let loc = update.location {
/// Update the Published variables
self.lastLocation = loc
self.isStationary = update.isStationary
self.count += 1
/// If we are not stationary then then process the lcation
if update.isStationary == false {
locationUpdateCounter += 1
if locationUpdateCounter >= UserSettings.init().trackingSampleRate {
saveLocationUpdates(location: self.lastLocation)
locationUpdateCounter = 0
}
}
}
}
} catch {
self.logger.error("** Could not start location updates")
}
return
}
}
func stopLocationUpdates() {
self.logger.info("** Stopping location updates")
self.updatesStarted = false
}
func saveLocationUpdates(location: CLLocation) {
do {
// Access the sharedModelContainer
guard let container = AppEnvironment.sharedModelContainer else {
LogEvent.print(module: "LocationsHandler.saveLocation()", message: "shared model container has not been initialized")
return
}
let context = ModelContext(container)
let entry = GpsJournalSD(
timestamp: Date(),
longitude: location.coordinate.longitude,
latitude: location.coordinate.latitude,
speed: location.speed,
processed: false,
code: "",
note: ""
)
context.insert(entry)
print("**+ Location saved: \(entry.timestamp) \(formatMPH(convertMPStoMPH( entry.speed))) mph")
}
}
}
My saveLocationUpdate just writes data to swiftdata and that works fine with the app is in the foreground/background (but doesn't update if app terminated)
Here is the code in the appDelegate:
class AppDelegate: NSObject, UIApplicationDelegate {
static let shared = AppDelegate()
class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject {
let logger = Logger(subsystem: "com.***.yyy", category: "AppDelegate")
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
/// Set as a singleton. Important otherwise you get competing location handlers
let locationsHandler = LocationsHandler.shared
// If location updates were previously active, restart them after the background launch.
if locationsHandler.updatesStarted {
self.logger.info("** Restart liveUpdates Session")
locationsHandler.startLocationUpdates()
}
// If a background activity session was previously active, reinstantiate it after the background launch.
if locationsHandler.backgroundActivity {
self.logger.info("** Reinstantiate background activity session")
locationsHandler.backgroundActivity = true
}
return true
}
}
}