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]) } } }
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]) } } }
Jul ’24
Reply to iPadOS Sidebar Split Navigation View with SwiftUI
@crystalminds in your example the app only shows 2 colums on every sidebar item, which is not the intended behavior. It should show the sidebar + 2 columns on sidebar item one and two. On sidebar item 3 there should only be the sidebar + 1 column. I have filed a radar/feedback for this and included all the information in this repository: https://github.com/JulianKahnert/NavigationExample
Aug ’20
Reply to SwiftUI App lifecycle
@Volker88 is totally right, you have to add @Environment(\.scenePhase) var scenePhase. So the full example should be: import SwiftUI @main struct TestApp: App {     @Environment(\.scenePhase) var scenePhase     var body: some Scene {         WindowGroup {             ContentView()         }         .onChange(of: scenePhase) { phase in             switch phase {             case .active:             print("Active")             case .inactive:                 print("Inactive")             case .background:                 print("Background")             @unknown default:                 print("Unknown")             }         }     } }
Jun ’20