Post

Replies

Boosts

Views

Activity

Reply to EnvironmentObject Causes SwiftUI App to Crash When Launched in the Background
Hi, just a cross reference to my answer on this thread. tl;dr: I could solve it by moving the view modifier from the WindowGroup to the ContentView. I have not tested it for the .environment view modifier. Non crashing example: import SwiftData import SwiftUI @main struct ExampleApp: App { var body: some Scene { WindowGroup { ContentView() .modelContainer(for: [Item.self]) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’24
Reply to Error: _SwiftData_SwiftUI: one-time initialization function for empty
Hi, I encountered the same issue! Steps to reproduce the crash: Environment: iOS 17.5.1 Use a template project with SwiftData using CloudKit (see Code 1) Install it via Testflight on two devices (A & B) Add items in the CloudKit container (on a regular basis) via Device A Download the App via Testflight on Device B and move the app after a few seconds to the background (not force closing the app) Crash after some time Fix: Move the .modelContainer(for: [Item.self]) ViewModifier from the WindowGroup to the ContentView. Assumption: The problem occurs when the App starts in the Background after a notification was received. Maybe the WindowGroup behaves differently when started in the background, because no window exists!? I could only reproduce it via a prod CloudKit environment @DTS Engineer if that's the case, maybe the template projects in Xcode should be updated, so new projects do not trigger that crash. Code 1 Crashing examaple: import SwiftData import SwiftUI @main struct ExampleApp: App { var body: some Scene { WindowGroup { ContentView() } .modelContainer(for: [Item.self]) } } Code 2 Fixed with: import SwiftData import SwiftUI @main struct ExampleApp: App { var body: some Scene { WindowGroup { ContentView() .modelContainer(for: [Item.self]) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’24