Two SwiftData @Query Pitfalls — enum #Predicate and modelContext on Empty Lists
The Setup I was building Inbox/Archive list views for a document scanning app. Simple enough: filter documents by status using SwiftData’s @Query. Two compile-time surprises were waiting. Pitfall 1: #Predicate Can’t Compare Enums What I Wrote The natural approach: @Model class Document { var status: DocumentStatus // enum stored property // ... } enum DocumentStatus: String, Codable { case inbox, archived } Then in the view: @Query( filter: #Predicate<Document> { $0.status == .inbox }, sort: \Document.createdAt, order: .reverse ) private var documents: [Document] Looks clean. Doesn’t compile. ...