firebase-ios-sdk
firebase-ios-sdk copied to clipboard
Failed to launch: Analytics.logEvent breaks SwiftUI Preview without warning [Xcode 16, Swift 6]
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
trackEventis never even called. - Changing
FirebaseAnalyticsServiceto astructorclassmakes 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!
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
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.
+1 for this issue. I have found running preview with iOS 17 device still works, even with new preview engine
@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
+1
+1
Workaround:
Editor -> Canvas -> Enable Legacy Previews Execution.
Found here: https://forums.developer.apple.com/forums/thread/756681
Workaround:
Editor -> Canvas -> Enable Legacy Previews Execution.Found here: https://forums.developer.apple.com/forums/thread/756681
It is helped me
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?
+1
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)
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.
Big kudos to the reporter for finding the exact cause! I was giving up...
Same issue -- just commenting for bumping's sake
+1
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.