Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bwised opened this issue Nov 17, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@bwised
Copy link

bwised commented Nov 17, 2024

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:

  • Ignore the runtime warning and suffer whatever the possible consequences are, or
  • Disable the main thread checker (not recommended), or
  • Ensure that the Amplitude object is created on the main thread, or
  • Downgrade to an Amplitude version prior to 1.10.4.

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

  • SDK Version: 1.10.4
  • OS Info: iOS -- the specific version is irrelevant
  • Xcode: Xcode 16.1 (perhaps other versions of Xcode do not emit this warning)
@bwised bwised added the bug Something isn't working label Nov 17, 2024
@bwised
Copy link
Author

bwised commented Nov 17, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant