Crash in Swift 6 when using UNUserNotification

After porting code to Swift 6 (Xcode 16.4), I get a consistent crash (on simulator) when using UNUserNotificationServiceConnection It seems (searching on the web) that others have met the same issue. Is it a known Swift6 bug ? Or am I misusing UNUserNotification ?

I do not have the crash when compiling on Xcode 26 ß5, which hints at an issue in Xcode 16.4.

Crash log:

Thread 10 Queue : com.apple.usernotifications.UNUserNotificationServiceConnection.call-out (serial)

As far as I can tell, it seems error is when calling

nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

I had to declare non isolated to solve a compiler error.

Main actor-isolated instance method 'userNotificationCenter(_:didReceive:withCompletionHandler:)' cannot be used to satisfy nonisolated requirement from protocol 'UNUserNotificationCenterDelegate'

I was advised to:

Add 'nonisolated' to 'userNotificationCenter(_:didReceive:withCompletionHandler:)' to make this instance method not isolated to the actor

I filed a bug report: Aug 10, 2025 at 2:43 PM – FB19519575

You are receiving the notification in an arbitrary background thread. Make didReceive nonisolated and do whatever you do inside it, in a MainActor isolated task. Do not change willPresent to nonisolated. It needs to be called on MainActor since it is a function about the user interface.

If you are posting the notification yourself, I recommend posting it on MainActor to get rid of most concurrency related issues. Be aware that the system and frameworks will continue posting from an arbitrary thread.

Thanks for the answer.

didReceive was already non isolated. So problem likely does not come from here.

For willPresent, if I don't make it nonIsolated, I get an Xcode 16.4 compiler error:

Main actor-isolated instance method 'userNotificationCenter(_:willPresent:withCompletionHandler:)' cannot be used to satisfy nonisolated requirement from protocol 'UNUserNotificationCenterDelegate'

n Xcode 26, no error in func declaration if I remove nonIsolated.

But error at UIViewController class definition level

Conformance of 'CalcViewController' to protocol 'UNUserNotificationCenterDelegate' crosses into main actor-isolated code and can cause data races

I would be very helpful if you were able to reproduce this on an actual device and attach a full symbolicated crash log to the bug report.

Also, if you are able to reproduce this with a small sample project, do attach that as well to help diagnose the issue.

@Engineer Thanks for reply. Some more crash info. On simulator (did not test on device at this stage).

Here is crash report:

Error occurs in #4 notifyMe IBAction

    @IBAction func notifyMe(_ sender: UIButton) {
        
        let center = UNUserNotificationCenter.current()
        center.delegate = self 
        center.getNotificationSettings(completionHandler: { (settings) in
            if settings.authorizationStatus == .notDetermined {
                center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
                    
                    DispatchQueue.main.async {  
                        if !granted {
					                 	// Alert user
                        }
                    }
                }
            } else if settings.authorizationStatus == .denied {
                DispatchQueue.main.async {
					        	// Alert user
                }
            } else if settings.authorizationStatus == .authorized {
                DispatchQueue.main.async {
					        self.alarmButton.isHidden = self.duree <= 120 
                }
            }
        })

        var alertStyle = UIAlertController.Style.alert
        
        let alertController = UIAlertController(
            title: NSLocalizedString("M'avertir", comment: ""),
            message: NSLocalizedString("Attention", comment: ""), preferredStyle: alertStyle)
        var title = NSLocalizedString("5'", comment: "") 
        let ok = UIAlertAction(title: title, style: .default, handler: {(action) -> Void in
            self.sendInitialNotification(beforeEnd: 5)
            DispatchQueue.main.async {  sender.isHidden = true  }
        })
        alertController.addAction(ok)
                
        let cancelAction = UIAlertAction(title: NSLocalizedString("Non", comment: ""), style: .default, handler: { (action) -> Void in
        
        alertController.addAction(cancelAction)

        present(alertController, animated: true, completion: nil)
         
    }

Crash apparently occurs on calling userNotificationCenter:

nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        
        if notification.request.identifier == requestIdentifier {
            completionHandler( [.alert,.sound,.badge])
        }
    }
Crash in Swift 6 when using UNUserNotification
 
 
Q