[PLAT-7702] Fix currentAppState when called before UIApplicationMain #1248
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Goal
Fix detection of foreground / background state when app is prewarmed.
If Bugsnag is started before
UIAppliationMain()
has been called (e.g. from a SwiftUI app'sinit()
function)UIApplication.sharedApplication
will be nil.When the
applicationState
message is send to nil, a value of 0 (i.e. active) is returned and causes Bugsnag to think the app is running in the foreground, triggering automatic session tracking and resulting in OOMs to be detected if the process gets terminated before the app is launched by the user.Changeset
Return
UIApplicationStateBackground
ifsharedApplication
is nil.Testing
To cause an app to be prewarmed, install and run on a device and then reboot it. The OS typically runs it soon after starting up again - within a minute on a device with few apps.
Manually verified with a SwiftUI sample app that a session is still started automatically at the correct time (when the relevant notification is sent) but not before the app is opened.
Did not find a way to replicate the false OOMs, because our OOM detection logic checks for and ignores reboots.
Amended unit tests that relied on
applicationState
to be active.