firebase / firebase-ios-sdk

Firebase SDK for Apple App Development
https://firebase.google.com
Apache License 2.0
5.68k stars 1.49k forks source link

Failed to launch: Analytics.logEvent breaks SwiftUI Preview without warning [Xcode 16, Swift 6] #13603

Open SwiftfulThinking opened 2 months ago

SwiftfulThinking commented 2 months ago

Description

The code Analytics.logEvent() causes a compile-time crash for all SwiftUI previews in a project, resulting in:

Failed to launch app in reasonable time.

This occurs as long as the code is anywhere in the codebase, regardless of whether it is executed in the current Preview. Therefore, it breaks all SwiftUI previews in a project and has no indicator to the source of the crash.

Reproducing the issue

import SwiftUI
import FirebaseAnalytics

actor FirebaseAnalyticsService {
    func trackEvent() {
        Analytics.logEvent("TEST", parameters: [:])
    }
}

struct ContentView: View {
    let service: FirebaseAnalyticsService

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
    }
}

#Preview {
    ContentView(service: FirebaseAnalyticsService())
}

Firebase SDK Version

11.1.0

Xcode Version

16.6 beta

Installation Method

Swift Package Manager

Firebase Product(s)

Analytics

Targeted Platforms

iOS

Relevant Log Output

Failed to launch app "___" in reasonable time

If using Swift Package Manager, the project's Package.resolved

Expand Package.resolved snippet
```json Replace this line with the contents of your Package.resolved. ```

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet
```yml Replace this line with the contents of your Podfile.lock! ```
google-oss-bot commented 2 months ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

ncooke3 commented 2 months ago

Thanks for filing, @SwiftfulThinking. We were able to reproduce this behavior. Xcode 16 introduced a new "preview engine" (release notes) so it seems likely that there is a regressions of some sort. I have filed a feedback ticket with Apple, FB15114904. Feel free to reference it if you happen to file one yourself.

Unfortunately, I do not have a workaround to share. I tried experimenting with detecting when Xcode was building for previews, in an effort to disable to problematic code, but was unable to come up with something that worked.

jordikitto commented 2 months ago

+1 for this issue. I have found running preview with iOS 17 device still works, even with new preview engine

vade commented 2 months ago

@ncooke3 not sure if this is helpful, but supposedly

        if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
        {
               // Disable offending code
        }

is a viable approach. Im also affected by a maybe similar issue on macOS 15.1 / Xcode 16.1 beta when previewing SwiftUI with Firebase and was able to work with the same project on macOS 14 / and earlier Xcodes.

I am on Swift 5 though

helloniklas commented 2 months ago

+1

nicodussio22 commented 2 months ago

+1

stephenfung98 commented 2 months ago

Workaround:

Editor -> Canvas -> Enable Legacy Previews Execution.

Found here: https://forums.developer.apple.com/forums/thread/756681

alexosnach commented 1 month ago

Workaround:

Editor -> Canvas -> Enable Legacy Previews Execution.

Found here: https://forums.developer.apple.com/forums/thread/756681

It is helped me

jordikitto commented 1 month ago

Enabling Legacy Preview engine is not an ideal fix though, as we would like to take advantage of the new preview engine for an improved development flow. Is there any further info on a potential real fix for this issue?

anton-alekseev-amo commented 1 month ago

+1

maiis commented 1 month ago

Workaround:

Editor -> Canvas -> Enable Legacy Previews Execution.

Found here: https://forums.developer.apple.com/forums/thread/756681

It appears to have been removed in the latest iOS beta. 😱 (Xcode 16.1 Beta 2)

vincekruger commented 1 month ago

OMG. The whole day I spent on this 😢

"Enable Legacy Previews Execution" doesn't work. This is the only way I've gotten around this error.

//
//  AnalyticsManager.swift
//

import FirebaseAnalytics

class AnalyticsManager {
    static func logEvent(_ event: String, parameters: [String: Any]?) {
        #if !targetEnvironment(simulator)
        Analytics.logEvent(event, parameters: parameters)
        #endif
    }
}

The error persists in all three of the different analytics frameworks.

Meyssam120 commented 1 month ago

Big kudos to the reporter for finding the exact cause! I was giving up...

blackxfiied commented 2 weeks ago

Same issue -- just commenting for bumping's sake

zchwyng commented 2 days ago

+1