Fix macOS thread checker warning

- Move all app state checks inside DispatchQueue.main.async
- Ensures NSApplication.isActive is only accessed from main thread
- Prevents thread checker warnings on macOS
This commit is contained in:
jack
2025-07-22 16:46:03 +02:00
parent 7b81dd4c0d
commit 29c0fdca21
+13 -14
View File
@@ -30,19 +30,7 @@ class NotificationService {
} }
func sendLocalNotification(title: String, body: String, identifier: String) { func sendLocalNotification(title: String, body: String, identifier: String) {
// Create notification content first (thread-safe) // Everything must be on main thread to check app state
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = .default
let request = UNNotificationRequest(
identifier: identifier,
content: content,
trigger: nil // Deliver immediately
)
// Check app state on main thread and send notification
DispatchQueue.main.async { DispatchQueue.main.async {
#if os(iOS) #if os(iOS)
guard UIApplication.shared.applicationState != .active else { guard UIApplication.shared.applicationState != .active else {
@@ -58,7 +46,18 @@ class NotificationService {
} }
#endif #endif
// App is in background/inactive, send notification // App is in background/inactive, create and send notification
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = .default
let request = UNNotificationRequest(
identifier: identifier,
content: content,
trigger: nil // Deliver immediately
)
UNUserNotificationCenter.current().add(request) { error in UNUserNotificationCenter.current().add(request) { error in
if let error = error { if let error = error {
print("📱 Error sending local notification: \(error)") print("📱 Error sending local notification: \(error)")