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])
}
}
}