Post

Replies

Boosts

Views

Activity

Reply to On Swift 6 func locationManager( _ manager: CLLocationManager, didUpdateLocations locations: [CLLocation] ) is never called
This is the full init: override init() { self.locationManager = CLLocationManager() super.init() self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation self.locationManager.distanceFilter = kCLDistanceFilterNone self.locationManager.headingFilter = kCLHeadingFilterNone self.locationManager.pausesLocationUpdatesAutomatically = false self.locationManager.delegate = self self.locationManager.startUpdatingHeading() self.locationManager.startUpdatingLocation() self.locationManager.requestWhenInUseAuthorization() self.currentLocation = self.locationManager.location }
Jul ’26
Reply to On Swift 6 func locationManager( _ manager: CLLocationManager, didUpdateLocations locations: [CLLocation] ) is never called
As a matter of fact, albeit that function is not called, for some strange reason at the end of the init function I find the value of the location at self.locationManager.location, for some magical reason, given it should not have had the time to check the location, if of course it was not taken before.
Jul ’26
Reply to Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution
I found a solution by making the closure sendable: func application( _ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping @Sendable () -> Void) { let runCompletionHandler = {@MainActor in completionHandler() } Task.init{ await GeoreferenceQueue.shared.setBackgroundCompletionHandler(runCompletionHandler) } } Now all errors seem to have vanished.
Topic: Programming Languages SubTopic: Swift Tags:
May ’26
Reply to Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution
I fixed the error in the actor by using this code: private var backgroundCompletionHandler: (() async -> Void)? and func getBackgroundCompletionHandler() -> ((() async -> Void)?) { return backgroundCompletionHandler } func setBackgroundCompletionHandler(_ h: (() -> Void)?){ backgroundCompletionHandler=h } func executeCompletionHandler() async{ await backgroundCompletionHandler?() } Yet my error on the app delegate at: func application( _ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { let runCompletionHandler = {@MainActor in completionHandler() } Task.init{ await GeoreferenceQueue.shared.setBackgroundCompletionHandler(runCompletionHandler) } } Converting function value of type '@MainActor @Sendable () -> Void' to '() -> Void' loses global actor 'MainActor' is still there. Now I set all functions in the actor to MainActor as well the variable storing the completion: @MainActor private var backgroundCompletionHandler: (() async -> Void)? @MainActor func getBackgroundCompletionHandler() -> ((() async -> Void)?) { return backgroundCompletionHandler } @MainActor func setBackgroundCompletionHandler(_ h: (() async -> Void)?){ backgroundCompletionHandler=h } @MainActor func executeCompletionHandler() async{ await backgroundCompletionHandler?() } and the error returned to the completionHandler() line of: func application( _ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { let runCompletionHandler = {@MainActor in completionHandler() } Task.init{ await GeoreferenceQueue.shared.setBackgroundCompletionHandler(runCompletionHandler) } } with: Task-isolated 'completionHandler' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses
Topic: Programming Languages SubTopic: Swift Tags:
May ’26
Reply to Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution
Unfortunately using your code as of: func application( _ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { let runCompletionHandler = { @MainActor in completionHandler() } Task.init{ await GeoreferenceQueue.shared.addCompletionHandler(runCompletionHandler) } } produces: Sending 'completionHandler' risks causing data races And @MainActor in your code is blue as it were recognized, in my code it remain black. And moreover in the actor at: func addCompletionHandler(_ h: @escaping (() async -> Void)) { backgroundCompletionHandler=h } I have: /Users/fbartolom/Documents/cocoa applications/iPuja/Classes/GeoreferenceQueue.swift:78:36 Invalid conversion from 'async' function of type '() async -> Void' to synchronous function type '() -> Void' On the assignment of h to the variable In practice previously I had an error message, now two...
Topic: Programming Languages SubTopic: Swift Tags:
May ’26
Reply to Type 'class' does not conform to protocol 'protocol'
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL){ do { //print("location=\(location)") let data = try Data(contentsOf: location, options: .uncachedRead) // Hold this file as an NSData and write it to the new location //submitCompletion?(data, nil) backgroundCompleted(data: data) } catch { backgroundCompleted(data: nil, error: error) print("could not get data for error \(error)") } } public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { Task.init{ let backgroundLocalizationActive=await GeoreferenceQueue.shared.getBackgroundLocalizationActive() await MainActor.run { if CLLocationManager.locationServicesEnabled() { if manager.authorizationStatus == .authorizedAlways && backgroundLocalizationActive{ if #available(iOS 17.0, *) { //let backgroundSession=CLBackgroundActivitySession() } else { // Fallback on earlier versions } self.locationManager.requestAlwaysAuthorization() self.locationManager.showsBackgroundLocationIndicator=true //let session=CLServiceSession(autorization: CLServiceSession.AuthorizationRequirement.always // } else if (manager.authorizationStatus == .authorizedWhenInUse){ self.locationManager.allowsBackgroundLocationUpdates = false self.locationManager.desiredAccuracy = 5; //self.locationManager!.desiredAccuracy=kCLLocationAccuracyBest self.locationManager.distanceFilter = kCLDistanceFilterNone self.locationManager.headingFilter = kCLHeadingFilterNone self.locationManager.pausesLocationUpdatesAutomatically=true; } } } print("start localizing") } } func locationManager( _ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) { let heading = newHeading.trueHeading > 0 ? newHeading.trueHeading : newHeading.magneticHeading locateUserAtHeading(heading: heading) } func handleLocationUpdate(_ newLocation:CLLocation, moved: Bool) async throws { //virtual } public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){ newDate=Date() //print("didUpdateLocation") var moved=false if let lastLocation=locations.first{ Task.init{ await SettingsActor.shared.setPresentLocation(lastLocation) //valore usato da discendenti } let timePast=newDate.timeIntervalSince(date) if timePast>30 { moved = lastLocation.distance(from: oldLocation)>50 date=newDate oldLocation=lastLocation } Task.init{ try? await self.handleLocationUpdate(lastLocation, moved: moved) } } } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { Task.init{ await MainActor.run{ let title=NSLocalizedString("Your location", comment:"") let message=String(format:NSLocalizedString( "I am not able to establish your location: please move somewhat possibly in an uncovered area", comment:""), CLLocation().altitude) let dismiss=NSLocalizedString("Dismiss", comment:"") let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) let cancelAction = UIAlertAction(title: dismiss, style: .cancel) { (action) in } alertController.addAction(cancelAction) } } } #if !limo func triggerActiveLocationUpdate(_ entering:Bool){ //virtual } func appWillResignActive(_ notification: Notification){ triggerActiveLocationUpdate(false) } func appWillEnterForeground(_ notification: Notification){ triggerActiveLocationUpdate(true) } #endif func debugNotification(_ message:String){ #if !NDDEBUG //localNotification(message) #endif } /*public func localNotification(_ message:String){ //for Objective -C self.localNotification(message, sound:nil, badge:nil) }*/ func stringUUID()->String{ let strUUID = UUID().uuidString return strUUID } func localNotification(_ title:String, message:String, sound:UNNotificationSound?, badge:Int?, interval:TimeInterval, userInfo: [AnyHashable : Any]?, category:UNNotificationCategory?=nil){ let center = UNUserNotificationCenter.current() let content = UNMutableNotificationContent() content.title = title content.body = message content.categoryIdentifier = "alarm" if category != nil{ UNUserNotificationCenter.current().setNotificationCategories([category!]) } if let isUserInfo=userInfo{ content.userInfo = isUserInfo } content.sound = sound //var dateComponents = DateComponents() //dateComponents.hour = 10 //dateComponents.minute = 30 //let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false) let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) center.add(request) } }
May ’26
Reply to On Swift 6 func locationManager( _ manager: CLLocationManager, didUpdateLocations locations: [CLLocation] ) is never called
This is the full init: override init() { self.locationManager = CLLocationManager() super.init() self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation self.locationManager.distanceFilter = kCLDistanceFilterNone self.locationManager.headingFilter = kCLHeadingFilterNone self.locationManager.pausesLocationUpdatesAutomatically = false self.locationManager.delegate = self self.locationManager.startUpdatingHeading() self.locationManager.startUpdatingLocation() self.locationManager.requestWhenInUseAuthorization() self.currentLocation = self.locationManager.location }
Replies
Boosts
Views
Activity
Jul ’26
Reply to On Swift 6 func locationManager( _ manager: CLLocationManager, didUpdateLocations locations: [CLLocation] ) is never called
As a matter of fact, albeit that function is not called, for some strange reason at the end of the init function I find the value of the location at self.locationManager.location, for some magical reason, given it should not have had the time to check the location, if of course it was not taken before.
Replies
Boosts
Views
Activity
Jul ’26
Reply to Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution
I found a solution by making the closure sendable: func application( _ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping @Sendable () -> Void) { let runCompletionHandler = {@MainActor in completionHandler() } Task.init{ await GeoreferenceQueue.shared.setBackgroundCompletionHandler(runCompletionHandler) } } Now all errors seem to have vanished.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’26
Reply to Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution
I fixed the error in the actor by using this code: private var backgroundCompletionHandler: (() async -> Void)? and func getBackgroundCompletionHandler() -> ((() async -> Void)?) { return backgroundCompletionHandler } func setBackgroundCompletionHandler(_ h: (() -> Void)?){ backgroundCompletionHandler=h } func executeCompletionHandler() async{ await backgroundCompletionHandler?() } Yet my error on the app delegate at: func application( _ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { let runCompletionHandler = {@MainActor in completionHandler() } Task.init{ await GeoreferenceQueue.shared.setBackgroundCompletionHandler(runCompletionHandler) } } Converting function value of type '@MainActor @Sendable () -> Void' to '() -> Void' loses global actor 'MainActor' is still there. Now I set all functions in the actor to MainActor as well the variable storing the completion: @MainActor private var backgroundCompletionHandler: (() async -> Void)? @MainActor func getBackgroundCompletionHandler() -> ((() async -> Void)?) { return backgroundCompletionHandler } @MainActor func setBackgroundCompletionHandler(_ h: (() async -> Void)?){ backgroundCompletionHandler=h } @MainActor func executeCompletionHandler() async{ await backgroundCompletionHandler?() } and the error returned to the completionHandler() line of: func application( _ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { let runCompletionHandler = {@MainActor in completionHandler() } Task.init{ await GeoreferenceQueue.shared.setBackgroundCompletionHandler(runCompletionHandler) } } with: Task-isolated 'completionHandler' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’26
Reply to Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution
Unfortunately using your code as of: func application( _ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { let runCompletionHandler = { @MainActor in completionHandler() } Task.init{ await GeoreferenceQueue.shared.addCompletionHandler(runCompletionHandler) } } produces: Sending 'completionHandler' risks causing data races And @MainActor in your code is blue as it were recognized, in my code it remain black. And moreover in the actor at: func addCompletionHandler(_ h: @escaping (() async -> Void)) { backgroundCompletionHandler=h } I have: /Users/fbartolom/Documents/cocoa applications/iPuja/Classes/GeoreferenceQueue.swift:78:36 Invalid conversion from 'async' function of type '() async -> Void' to synchronous function type '() -> Void' On the assignment of h to the variable In practice previously I had an error message, now two...
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’26
Reply to Type 'class' does not conform to protocol 'protocol'
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL){ do { //print("location=\(location)") let data = try Data(contentsOf: location, options: .uncachedRead) // Hold this file as an NSData and write it to the new location //submitCompletion?(data, nil) backgroundCompleted(data: data) } catch { backgroundCompleted(data: nil, error: error) print("could not get data for error \(error)") } } public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { Task.init{ let backgroundLocalizationActive=await GeoreferenceQueue.shared.getBackgroundLocalizationActive() await MainActor.run { if CLLocationManager.locationServicesEnabled() { if manager.authorizationStatus == .authorizedAlways && backgroundLocalizationActive{ if #available(iOS 17.0, *) { //let backgroundSession=CLBackgroundActivitySession() } else { // Fallback on earlier versions } self.locationManager.requestAlwaysAuthorization() self.locationManager.showsBackgroundLocationIndicator=true //let session=CLServiceSession(autorization: CLServiceSession.AuthorizationRequirement.always // } else if (manager.authorizationStatus == .authorizedWhenInUse){ self.locationManager.allowsBackgroundLocationUpdates = false self.locationManager.desiredAccuracy = 5; //self.locationManager!.desiredAccuracy=kCLLocationAccuracyBest self.locationManager.distanceFilter = kCLDistanceFilterNone self.locationManager.headingFilter = kCLHeadingFilterNone self.locationManager.pausesLocationUpdatesAutomatically=true; } } } print("start localizing") } } func locationManager( _ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) { let heading = newHeading.trueHeading > 0 ? newHeading.trueHeading : newHeading.magneticHeading locateUserAtHeading(heading: heading) } func handleLocationUpdate(_ newLocation:CLLocation, moved: Bool) async throws { //virtual } public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){ newDate=Date() //print("didUpdateLocation") var moved=false if let lastLocation=locations.first{ Task.init{ await SettingsActor.shared.setPresentLocation(lastLocation) //valore usato da discendenti } let timePast=newDate.timeIntervalSince(date) if timePast>30 { moved = lastLocation.distance(from: oldLocation)>50 date=newDate oldLocation=lastLocation } Task.init{ try? await self.handleLocationUpdate(lastLocation, moved: moved) } } } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { Task.init{ await MainActor.run{ let title=NSLocalizedString("Your location", comment:"") let message=String(format:NSLocalizedString( "I am not able to establish your location: please move somewhat possibly in an uncovered area", comment:""), CLLocation().altitude) let dismiss=NSLocalizedString("Dismiss", comment:"") let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) let cancelAction = UIAlertAction(title: dismiss, style: .cancel) { (action) in } alertController.addAction(cancelAction) } } } #if !limo func triggerActiveLocationUpdate(_ entering:Bool){ //virtual } func appWillResignActive(_ notification: Notification){ triggerActiveLocationUpdate(false) } func appWillEnterForeground(_ notification: Notification){ triggerActiveLocationUpdate(true) } #endif func debugNotification(_ message:String){ #if !NDDEBUG //localNotification(message) #endif } /*public func localNotification(_ message:String){ //for Objective -C self.localNotification(message, sound:nil, badge:nil) }*/ func stringUUID()->String{ let strUUID = UUID().uuidString return strUUID } func localNotification(_ title:String, message:String, sound:UNNotificationSound?, badge:Int?, interval:TimeInterval, userInfo: [AnyHashable : Any]?, category:UNNotificationCategory?=nil){ let center = UNUserNotificationCenter.current() let content = UNMutableNotificationContent() content.title = title content.body = message content.categoryIdentifier = "alarm" if category != nil{ UNUserNotificationCenter.current().setNotificationCategories([category!]) } if let isUserInfo=userInfo{ content.userInfo = isUserInfo } content.sound = sound //var dateComponents = DateComponents() //dateComponents.hour = 10 //dateComponents.minute = 30 //let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false) let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) center.add(request) } }
Replies
Boosts
Views
Activity
May ’26