mixpanel / mixpanel-swift

Official iOS (Swift) Tracking Library for Mixpanel Analytics
https://mixpanel.com
Apache License 2.0
434 stars 234 forks source link

SwiftUI - Global accent color override #522

Closed esreli closed 2 years ago

esreli commented 2 years ago

Environment

Xcode Version 13.2.1 (13C100) Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30) SPM Resolved Dependency:

      {
        "package": "Mixpanel",
        "repositoryURL": "https://github.com/mixpanel/mixpanel-swift",
        "state": {
          "branch": "master",
          "revision": "8f6fcb1876f48ef4e7794c66719b23838c990410",
          "version": null
        }
      }

Issue

I'm building a SwiftUI iOS app with a min deployment of iOS 14+. Because of this I'm using the SwiftUI types introduced in iOS 14 like App, Scene, etc. As such, I'm not notified that the app has launched through the app or scene delegate as is the case for UIKit apps. With this "pure" SwiftUI approach, all initializations are typically done within the App's init function.

With the initialization of Mixpanel, all of a sudden the project's Global accent color (contained by an Asset Catalog) is reverted to the default system blue.

@main
struct MyApp: App {

    init() {
        // Initialize Mixpanel
        Mixpanel.initialize(token: .mixpanelToken)
        #if DEBUG
        Mixpanel.mainInstance().loggingEnabled = true
        #endif
    }

    var body: some Scene {
        WindowGroup {
            MyAppView()
        }
    }
}

I believe this is an issue with the initialization API because when I comment out the line...

// Mixpanel.initialize(token: .mixpanelToken)

...the app global accent color works as expected.

What I've tried to remedy the issue

Current Workaround

I've created a lazily built reference to Mixpanel.mainInstance() in which I initialize the token. This allows the Mixpanel initialize token code to be ran on the first time the app fires off the first track event.

lazy var instance: MixpanelInstance = {
    // Mixpanel
    Mixpanel.initialize(token: .mixpanelToken)
    #if DEBUG
    Mixpanel.mainInstance().loggingEnabled = true
    #endif
    return Mixpanel.mainInstance()
}()

This isn't ideal because any tracking associated with initializing Mixpanel is deferred until the first actively tracked event leaving a gap between launch and the first event in unknown territory.


Do you have any idea what's going on here? Thanks!

jaredmixpanel commented 2 years ago

@esreli can you provide a simple, example project that consistently reproduces this issue? I've managed to reproduce a few times, somewhat randomly, but more often than not, the issue is not reproducible.

esreli commented 2 years ago

Hi @jaredmixpanel, thanks for looking into this issue. After this sprint I'll try to set aside some time to publish a consistent repro case.

esreli commented 2 years ago

Hi @jaredmixpanel. I've reproduced the bug and pushed the code to this repository: esreli/mixpanel-bug.

Here's another interesting thing that I've learned along the way. If I were to initialize Mixpanel in the highest level View instead of initializing Mixpanel within the App then the problem seems to resolve itself.

Thanks in advance for looking into this for me!

jaredmixpanel commented 2 years ago

@esreli thanks for the example project. I still find the behavior very random. I've attached a long, boring, low resolution (sorry!) screen recording below, so you can see my results are very inconsistent. At the moment, I'm inclined to think this is a SwiftUI/Xcode kind of thing and that Mixpanel just so happens to be the code that is in your init(), but that some other code may cause similar issues.

https://user-images.githubusercontent.com/10504508/158898751-9a33c130-d451-4e35-853b-3c0a0f13543b.mp4

esreli commented 2 years ago

@jaredmixpanel very interesting. I've updated the dependency to the latest commit on main as of today and it appears that the issue was possibly connected to the deadlock issue and is now resolved.

I can't be entirely confident here because the bug/repro case is so unreliable but, for now, I believe the issue has been fixed. Thanks @gabriellanata!

Closing.

loryhuz commented 1 year ago

Hi @jaredmixpanel @esreli I've still the issue, my whole app turns blue when I init Mixpanel