swiftdata

Provides expert guidance on SwiftData schema design, querying, migrations, and CloudKit sync.

5|Updated Sep 24, 2017
One-click install
npx skills add https://github.com/madyankin/dotfiles --skill swiftdata-madyankin
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: swiftdata
Source: https://github.com/madyankin/dotfiles/tree/main/.config/agents/skills/swiftdata
Command: npx skills add https://github.com/madyankin/dotfiles --skill swiftdata-madyankin

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building SwiftData-backed apps involves subtle pitfalls like missing inverse relationships, thread-unsafe context usage, store-breaking schema changes, and CloudKit constraint violations that cause crashes or silent data corruption. This Skill provides expert architectural guidance and proven patterns to avoid these issues. ## Core Features & Use Cases - Schema & Relationship Design: Advises on @Model definitions, @Attribute options, bidirectional inverse relationships, and delete rules to prevent data corruption. - Context & Query Patterns: Covers ModelContainer setup, main vs background ModelContext usage, @Query with #Predicate filtering, and FetchDescriptor for imperative fetches. - Migrations & CloudKit: Deep-dive references on VersionedSchema, SchemaMigrationPlan lightweight and custom stages, plus CloudKit constraints like optional-only properties and last-write-wins conflicts. - Use Case: When adding a new non-optional property to a shipped SwiftData model, get guidance on whether a lightweight migration suffices or a custom MigrationStage with willMigrate/didMigrate hooks is required, including test code for the migration. ## Quick Start Ask how to design a SwiftData schema with relationships and migrations for your SwiftUI app, or request help fixing a specific SwiftData issue like a broken @Query or CloudKit crash.

Frequently Asked Questions about swiftdata

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up SwiftData with SwiftUI using @Query?▼

Register a ModelContainer at the app entry point with .modelContainer(for: [Book.self]), then use @Query in views for live fetches that re-render on data changes. Access the context via @Environment(\.modelContext) for inserts and deletes.

How do SwiftData migrations work with SchemaMigrationPlan?▼

Define each schema version as a VersionedSchema enum, then list MigrationStage entries in a SchemaMigrationPlan from oldest to newest. Lightweight stages handle adding optional properties and renames; custom stages with willMigrate and didMigrate handle data transformations.

Does SwiftData CloudKit sync support unique attributes?▼

No, CloudKit does not support @Attribute(.unique) and crashes at container initialization if used. Enforce uniqueness in app logic with a FetchDescriptor check before inserting, and make all stored properties optional for CloudKit compatibility.

Why does my SwiftData relationship cause data corruption?▼

SwiftData requires every relationship to declare an inverse on both sides; omitting it causes silent data corruption and broken cascades. Add inverse: \Book.shelf to the @Relationship on the parent and a matching optional reference on the child.

When should I use a background ModelContext in SwiftData?▼

Use a background ModelContext created via ModelContext(container) for bulk inserts or imports, since the main context is @MainActor-bound and blocks the UI. Disable autosaveEnabled and call save() once after the batch for best performance.

How do I test SwiftData code without a real database?▼

Create a ModelContainer with ModelConfiguration(isStoredInMemoryOnly: true) and a ModelContext from it in your test setup. This skips persistence and CloudKit entirely, letting you insert, fetch, and assert on @Model objects in isolation.