From 7b71d129f6b71e6a7978c934cda60f8190316674 Mon Sep 17 00:00:00 2001 From: Chris Leonavicius Date: Mon, 18 Nov 2024 12:51:07 -0800 Subject: [PATCH] fix: prevent main thread checker warning (#248) --- Sources/Amplitude/Plugins/iOS/IOSLifecycleMonitor.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/Amplitude/Plugins/iOS/IOSLifecycleMonitor.swift b/Sources/Amplitude/Plugins/iOS/IOSLifecycleMonitor.swift index ced89eb..cef0e04 100644 --- a/Sources/Amplitude/Plugins/iOS/IOSLifecycleMonitor.swift +++ b/Sources/Amplitude/Plugins/iOS/IOSLifecycleMonitor.swift @@ -41,7 +41,12 @@ class IOSLifecycleMonitor: UtilityPlugin { utils = DefaultEventUtils(amplitude: amplitude) // If we are already in the foreground, dispatch installed / opened events now - if IOSVendorSystem.sharedApplication?.applicationState == .active { + // Use keypath vs applicationState property to avoid main thread checker warning, + // we want to dispatch this from the initiating thread to maintain event ordering. + if let application = IOSVendorSystem.sharedApplication, + let rawState = application.value(forKey: #keyPath(UIApplication.applicationState)) as? Int, + let applicationState = UIApplication.State(rawValue: rawState), + applicationState == .active { utils?.trackAppUpdatedInstalledEvent() amplitude.onEnterForeground(timestamp: currentTimestamp) utils?.trackAppOpenedEvent()