Skip to content

Fix app_start trace not firing in SwiftUI apps using @UIApplicationDelegateAdaptor (#15802)#15912

Open
JesusRojass wants to merge 5 commits intofirebase:mainfrom
JesusRojass:JesusRojass/#15802
Open

Fix app_start trace not firing in SwiftUI apps using @UIApplicationDelegateAdaptor (#15802)#15912
JesusRojass wants to merge 5 commits intofirebase:mainfrom
JesusRojass:JesusRojass/#15802

Conversation

@JesusRojass
Copy link
Contributor

Description

Fixes #15802

SwiftUI apps using @UIApplicationDelegateAdaptor report applicationState as .background during didFinishLaunchingWithOptions even on normal foreground launches, because scenes haven't connected yet. This caused the _as (app start) trace to be incorrectly skipped.

Changes

Defer the background launch decision from didFinishLaunching to didBecomeActive. Record sLaunchedInBackgroundState at launch time and use the existing 60-minute threshold (gAppStartMaxValidDuration) to distinguish genuine background launches from SwiftUI's false .background report.

Testing

Verified with a SwiftUI test app using @UIApplicationDelegateAdaptor:

  • Foreground launch: _as trace fires correctly (~580ms)
  • Background fetch launch: no trace created (correct)
  • Existing prewarm and duration-threshold logic unchanged

API Changes

  • No API changes

@gemini-code-assist
Copy link
Contributor

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

@JesusRojass
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses an issue where app start traces were not firing correctly in SwiftUI applications that use @UIApplicationDelegateAdaptor. The fix cleverly defers the decision of whether a launch occurred in the background from applicationDidFinishLaunching to appDidBecomeActive, using a time threshold to distinguish between a genuine background launch and the false positive from SwiftUI. The implementation is clean and well-commented. I have one minor suggestion to improve a comment for better clarity.

Comment on lines +269 to +270
// App launched in background so we invalidate the captured app start time
// to prevent incorrect measurement when user later opens the app
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

This comment is a bit misleading as it appears to be a leftover from the previous implementation. The code no longer invalidates appStartTime. It just skips the app start trace if it's determined to be a genuine background launch. Consider updating the comment to more accurately reflect the current logic.

Suggested change
// App launched in background so we invalidate the captured app start time
// to prevent incorrect measurement when user later opens the app
// If the time since launch is too long, it's a genuine background launch.

@JesusRojass
Copy link
Contributor Author

Ready for review @visumickey 😄 💯

// Distinguish genuine background launches by checking how long ago +load captured appStartTime.
// A real foreground launch reaches didBecomeActive within seconds; a background launch that is
// later opened by the user will have a much larger gap.
if (sLaunchedInBackgroundState) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worried about a scenario where a non-switftUI app starts itself in background, but the app gets launched before the max app start duration value - this code would account for that app start time.

I think the ideal would be differentiate if this is a SwiftUI app and make some exceptions rather than impacting all the scenarios.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a commit that checks for UIApplicationSceneManifest in the info.plist before deferring to keep the original behavior in the rest of the escenarios

LMK what you think 😄

@JesusRojass JesusRojass requested a review from visumickey March 6, 2026 18:47
@JesusRojass
Copy link
Contributor Author

Comment addressed!

LMK what you think @visumickey 😄

Copy link
Member

@ncooke3 ncooke3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Please also include a changelog entry, under a new # Unreleased header.

@JesusRojass JesusRojass requested a review from ncooke3 March 9, 2026 16:18
@JesusRojass
Copy link
Contributor Author

thanks @ncooke3
added!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Firebase Performance app_start not firing in SwiftUI apps

3 participants