From 414b7aec5b0541d0f57502d60ca1177d64502436 Mon Sep 17 00:00:00 2001 From: Matt Hayashida Date: Wed, 11 Dec 2024 13:31:50 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Update=20PushMonitor=20to=20trac?= =?UTF-8?q?k=20new=20status=20on=20main=20thread?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tracking an event at startup from a background thread can cause data races --- Sources/AppcuesKit/Push/PushMonitor.swift | 28 +++++++++++++---------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Sources/AppcuesKit/Push/PushMonitor.swift b/Sources/AppcuesKit/Push/PushMonitor.swift index d1dca7acf..c67cb82fa 100644 --- a/Sources/AppcuesKit/Push/PushMonitor.swift +++ b/Sources/AppcuesKit/Push/PushMonitor.swift @@ -87,17 +87,9 @@ internal class PushMonitor: PushMonitoring { } func refreshPushStatus(completion: ((UNAuthorizationStatus) -> Void)? = nil) { - // Skip call to UNUserNotificationCenter.current() in tests to avoid crashing in package tests - #if DEBUG - guard ProcessInfo.processInfo.environment["XCTestBundlePath"] == nil else { - completion?(pushAuthorizationStatus) - return - } - #endif - - UNUserNotificationCenter.current().getNotificationSettings { [weak self] settings in - let shouldPublish = self?.appcues?.sessionID != nil && self?.pushAuthorizationStatus != settings.authorizationStatus - self?.pushAuthorizationStatus = settings.authorizationStatus + let handler: ((UNAuthorizationStatus) -> Void) = { [weak self] newStatus in + let shouldPublish = self?.appcues?.sessionID != nil && self?.pushAuthorizationStatus != newStatus + self?.pushAuthorizationStatus = newStatus if shouldPublish { self?.analyticsPublisher.publish(TrackingUpdate( @@ -106,7 +98,19 @@ internal class PushMonitor: PushMonitoring { )) } - completion?(settings.authorizationStatus) + completion?(newStatus) + } + + // Skip call to UNUserNotificationCenter.current() in tests to avoid crashing in package tests + #if DEBUG + guard ProcessInfo.processInfo.environment["XCTestBundlePath"] == nil else { + handler(pushAuthorizationStatus) + return + } + #endif + + UNUserNotificationCenter.current().getNotificationSettings { settings in + DispatchQueue.main.async { handler(settings.authorizationStatus) } } }