amplitude / Amplitude-Swift

Native iOS/tvOS/macOS/watchOS SDK
MIT License
33 stars 24 forks source link

Version `1.10.4` introduced a runtime warning when `setup(amplitude:)` is called #246

Open bwised opened 5 hours ago

bwised commented 5 hours ago

Expected Behavior

Constructing the Amplitude object exhibits no runtime warnings when called on a background thread. Application developers should not be forced to initialize third party SDKs on the main thread as that reduces our ability to optimize our launch experience for our users.

Current Behavior

Prior to version 1.10.4, constructing the Amplitude object on a background thread was warning free.

However, starting in 1.10.4 there is now a runtime warning emitted by Xcode's "main thread checker" on IOSLifecycleMonitor.setup(amplitude:) when Amplitude is constructed on a background thread. This is because setup(amplitude:) is now accessing the applicationState property from a UIApplication instance.

Possible Solution

In order to detach the ability to know what the current application state is at any time regardless of what thread is checking, there needs to be a high-level monitor that listens for application state changes (which would occur on the main thread) and update a primitive property in that monitor. Anything that needs to know the application state uses the monitor's value instead of reading applicationState from a UIApplication instance.

Until this bug is fixed, we have to do one of the following:

Steps to Reproduce

This is a universal example -- not my actual implementation.

func setupMyAmplitude() {
   DispatchQueue.global(qos: .userInitiated).async {
      let amplitude = Amplitude(configuration: configuration)
      ...
   }
}

This results in this emission from the main thread checker at runtime:

Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 45798, TID: 49052555, Thread name: (none), Queue name: com.apple.root.default-qos, QoS: 0
Backtrace:
4   AmplitudeSwift                      0x0000000107182ae0 $s14AmplitudeSwift19IOSLifecycleMonitorC5setup9amplitudeyAA0A0C_tF + 256
5   AmplitudeSwift                      0x000000010713cedc $s14AmplitudeSwift13UtilityPluginCAA0D0A2aDP5setup9amplitudeyAA0A0C_tFTW + 20
6   AmplitudeSwift                      0x0000000107122618 $s14AmplitudeSwift0A0C3add6pluginAcA6Plugin_p_tF + 112
7   AmplitudeSwift                      0x00000001071208f8 $s14AmplitudeSwift0A0C13configurationAcA13ConfigurationC_tcfc + 2416
8   AmplitudeSwift                      0x000000010711ff78 $s14AmplitudeSwift0A0C13configurationAcA13ConfigurationC_tcfC + 56
...

Environment

bwised commented 3 hours ago

For now, I've downgraded to 1.10.3 and have my Podfile set to that fixed version to prevent further upgrades. I will manually take a look at future updates to see if the issue is resolved. I may also issue my own pull request, time permitting.