Invalidating the CLBackgroundActivitySession some time the app has been killed
Do we need to explicitly do something about this? I don't believe we are performing any specific actions when the app gets terminated.
Not properly joining the existing CLBackgroundActivitySession from the last session by recreating one when launched in the background.
How is joining the existing CLBackgroundActivitySession from the last session?
I am currently referencing the following documentation: Adopting Live Updates in Core Location
At present, I am recreating the CLBackgroundActivitySession upon app relaunched.
(I believe that I am appropriately handling the restart from a Terminated state in application(_:didFinishLaunchingWithOptions:))
Here is the code being used:
class AppDelegate: NSObject, UIApplicationDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
if UserDefaults.standard.bool(forKey: "geofencing") {
Geofencing.shared.start()
}
}
}
class Geofencing {
private var monitorTask: Task<Void, Error>?
private var backgroundSession: CLBackgroundActivitySession?
func start() {
UserDefaults.standard.set(true, forKey: "geofencing")
if backgroundSession == nil {
backgroundSession = CLBackgroundActivitySession()
}
if monitorTask != nil {
monitorTask?.cancel()
monitorTask = nil
}
monitorTask = Task {
let monitor = await monitor
for try await event in await monitor.events {
handleEvent(event: event)
}
}
}
}
Topic:
App & System Services
SubTopic:
Maps & Location
Tags: