I'm attempting to leverage notifications in an app that is in Swift 6 language mode. I have the following code:
func startLocationUpdates() {
//if self.manager.authorizationStatus == .notDetermined {
// self.manager.requestWhenInUseAuthorization()
//}
self.logger.info("Starting location updates")
Task {
do {
let updates = CLLocationUpdate.liveUpdates()
for try await update in updates {
if !self.updatesStarted { break } // End location updates by breaking out of the loop.
self.lastUpdate = update
if let loc = update.location {
self.lastLocation = loc
self.isStationary = update.stationary
self.count += 1
self.logger.info("Location \(self.count): \(self.lastLocation)")
}
if lastUpdate!.insufficientlyInUse {
let notification = UNNotificationRequest(identifier: "com.example.mynotification", content: notificationContent, trigger: nil)
try await UNUserNotificationCenter.current().add(notification)
}
}
} catch {
self.logger.error("Could not start location updates")
}
return
}
}
As an aside, the above is directly taken from the following sample:
https://developer.apple.com/documentation/CoreLocation/adopting-live-updates-in-core-location.
With Swift 6 language mode enable, this generates a compiler error for the statement:
try await UNUserNotificationCenter.current().add(notification)
Sending main actor-isolated 'notification' to nonisolated instance method 'add' risks causing data races between nonisolated and main actor-isolated uses
How can I fix this?