SwiftData — Foundations Skill
Purpose
Route SwiftData implementation tasks to the minimum required SwiftData
Knowledge Contracts. v1 scope is declaring @Model classes and their
attributes/relationships, setting up and injecting a ModelContainer,
performing CRUD through a ModelContext, fetching with @Query or
FetchDescriptor, and delete-rule/referential-integrity behavior on
relationships -- not CloudKit sync, not schema migration, not Core Data
interop, and not the #Index/#Unique macros beyond a basic mention.
Routing
Load only the contracts relevant to the task. All paths relative to knowledge/swiftdata/.
- Annotating a class with
@Model; customizing a property with@Attribute(e.g..unique) or@Transient; declaring a@Relationshipproperty and itsinverse:; or asking what SwiftData auto-synthesizes (Identifiable/Hashable/Observable, notCodable) -> model-definition.md - Setting up
.modelContainer(for:)/.modelContainer(_:); configuringModelConfiguration(isStoredInMemoryOnly,allowsSave); creating aModelContainer(for:configurations:)manually for previews/tests/non-SwiftUI code; or wiring@Environment(\.modelContext)-> model-container-setup.md - Calling
context.insert(_:)/delete(_:)/save(); deciding whetherautosaveEnabledmakes an explicitsave()unnecessary; choosingmainContextvs. a manually created secondaryModelContext; or enabling undo viacontext.undoManager-> model-context-crud.md - Fetching with
@Query(filter:/sort:/order:/animation:) in a SwiftUI view; building a#Predicate; or fetching outside a view withFetchDescriptor<Model>+context.fetch(_:)-> querying-with-query-and-fetchdescriptor.md - Choosing a
@Relationship(deleteRule:)value (.cascade/.nullify/.deny/.noAction); reasoning about what happens to related models on delete; or diagnosing a one-directional relationship missing itsinverse:-> relationships-and-cascade-delete.md
Never load more than the contracts relevant to the specific question.
Stop Conditions
Stop and report if the requested topic has no matching Knowledge
Contract in knowledge/swiftdata/ — do not guess or fall back to general
knowledge. CloudKit sync integration (ModelConfiguration(cloudKitDatabase:))
is out of scope entirely -- not built as a contract here. Schema migration
(SchemaMigrationPlan, VersionedSchema, lightweight vs. custom migration)
is out of scope entirely -- not yet built. Core Data interop or migration
(NSManagedObjectContext, NSFetchRequest) belongs to a separate Core
Data domain, not this one -- do not answer Core Data questions from
SwiftData contracts or fabricate a shared API surface. The #Index/
#Unique freestanding macros beyond the basic @Attribute(.unique) case,
and SwiftData in widget extensions or App Group container sharing, are
out of scope entirely -- report the boundary rather than fabricate
behavior.