firebase-ios-sdk icon indicating copy to clipboard operation
firebase-ios-sdk copied to clipboard

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

Open SwiftfulThinking opened this issue 1 year ago • 13 comments

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.

  • It breaks the SwiftUI Preview but still compiles and runs successfully when building to a simulator.
  • It may be a threading issue related to updates in Swift 6.

Reproducing the issue

  • The below code will reproduce this issue 100% of the time.
  • Notice that trackEvent is never even called.
  • Changing FirebaseAnalyticsService to a struct or class makes the problem only semi-reproducible (if you restart and reset the canvas cache, only sometimes it will crash).
  • Since it crashes 100% of the time as an actor, it leads me to believe it's a threading or actor-related 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

Replace this line with the contents of your Package.resolved.

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet

Replace this line with the contents of your Podfile.lock!

SwiftfulThinking avatar Sep 08 '24 22:09 SwiftfulThinking

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

google-oss-bot avatar Sep 08 '24 22:09 google-oss-bot

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.

ncooke3 avatar Sep 12 '24 19:09 ncooke3

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

jordikitto avatar Sep 12 '24 23:09 jordikitto

@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

vade avatar Sep 14 '24 03:09 vade

+1

helloniklas avatar Sep 17 '24 12:09 helloniklas

+1

nicodussio22 avatar Sep 17 '24 15:09 nicodussio22

Workaround:

Editor -> Canvas -> Enable Legacy Previews Execution.

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

stephenfung98 avatar Sep 19 '24 02:09 stephenfung98

Workaround:

Editor -> Canvas -> Enable Legacy Previews Execution.

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

It is helped me

alexosnach avatar Sep 29 '24 09:09 alexosnach

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?

jordikitto avatar Oct 01 '24 00:10 jordikitto

+1

anton-alekseev-amo avatar Oct 01 '24 12:10 anton-alekseev-amo

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)

maiis avatar Oct 01 '24 17:10 maiis

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.

vincekruger avatar Oct 10 '24 20:10 vincekruger

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

Meyssam120 avatar Oct 17 '24 18:10 Meyssam120

Same issue -- just commenting for bumping's sake

vapidinfinity avatar Nov 09 '24 01:11 vapidinfinity

+1

zchwyng avatar Nov 23 '24 17:11 zchwyng

Hi everyone, recent Xcode changelogs have mentioned various SwiftUI Previews fixes. My original repro project successfully loads previews in the latest Xcode 16.2. I recommend to try again with the latest Xcode version. I'm going to close this, but will re-open as needed.

ncooke3 avatar Dec 18 '24 21:12 ncooke3