Post

Replies

Boosts

Views

Activity

Comment on EnvironmentObject Causes SwiftUI App to Crash When Launched in the Background
Thanks for this suggestion, I'll experiment with it. I will probably end up using my current workaround of handling the optional in the root view and returning an EmptyView if it's nil, because in my actual app (as opposed to the example I shared), the object is somewhat large and has several properties that don't have obvious default values. But the EnvironmentKey approach seems like a good alternative.
May ’24
Comment on EnvironmentObject Causes SwiftUI App to Crash When Launched in the Background
I was able to avoid instantiating the root ContentView by adding an @Environment(.scenePhase) property to the App struct, and then wrapping the ContentView in an "if" block that checked if case .background = scenePhase. However, I wasn't comfortable with this, because I thought it might have unintended consequences when moving from foreground to background. onChange(of: scenePhase) doesn't seem to be called when launching into the background, so I stuck with using optional Environment values.
Apr ’24