swiftdata-expert

Expert guidance for SwiftData on iOS 17+ and macOS 14+. Use when the user asks about SwiftData models, relationships, queries, predicates, ModelContext, ModelContainer, CloudKit sync, schema migrations, SwiftUI integration with @Query or @Bindable, performance optimization, testing with in-memory containers, or debugging silent save failures and crashes related to SwiftData.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "swiftdata-expert" with this command: npx skills add kashiash/swiftdata-expert-skill/kashiash-swiftdata-expert-skill-swiftdata-expert

SwiftData Expert Skill

Version: 1.0.0
Target: iOS 17+, macOS 14+, SwiftUI + SwiftData projects

Agent Behavior Contract

  1. Triage first: Use 60-second assessment before diving into code
  2. Route correctly: Match problem to appropriate section
  3. Verify assumptions: Check model definitions, relationships, query syntax
  4. Test incrementally: Validate each change with diagnostics
  5. Document decisions: Explain why specific patterns were chosen
  6. Handle errors gracefully: SwiftData fails silently - always verify saves
  7. Respect constraints: CloudKit, relationships, and migrations have strict rules

First 60 Seconds Triage

When user reports SwiftData issue:

1. IDENTIFY SYMPTOM
   - Data not saving? → Check autosave, context, container setup
   - Crash on launch? → Model definition, relationship, migration issue
   - Query returns empty? → Predicate syntax, model registration
   - UI not updating? → @Query usage, observation, context
   - CloudKit sync failing? → Optional properties, unique constraints

2. LOCATE PROBLEM AREA
   - Models (01) → @Model, properties, initializers
   - Relationships (02) → @Relationship, delete rules, inverse
   - Queries (03) → @Query, #Predicate, FetchDescriptor
   - Context/Container (04) → ModelContext, ModelContainer, saves
   - SwiftUI Integration (05) → @Query, @Bindable, environment
   - Performance (06) → Batch operations, faulting, indexes
   - CloudKit (07) → Configuration, optional properties, sync

3. VERIFY BASICS
   - Is @Model applied to class?
   - Is modelContainer() in App struct?
   - Are relationships properly defined?
   - Is context being used correctly?
   - Are saves happening (autosave vs manual)?

Routing Map

User SaysGo ToCommon Fixes
"Data not persisting"04-context-saves.mdEnable autosave, call save() manually
"Crash with relationships"02-relationships.mdAdd @Relationship, fix delete rules
"Query returns nothing"03-queries-predicates.mdFix predicate syntax, check model registration
"UI not updating"05-swiftui-integration.mdUse @Query, check @Bindable
"CloudKit not syncing"07-cloudkit-sync.mdMake properties optional, remove @Attribute(.unique)
"Migration failed"08-migrations.mdDefine migration plan, handle schema changes
"Performance issues"06-performance.mdAdd batch operations, optimize queries
"Network data not saving"10-network-integration.mdUse MainActor for inserts, handle Codable

Common Errors → Next Best Move

// ERROR: "Circular reference resolving attached macro 'Relationship'"
// FIX: Only define @Relationship on ONE side of relationship

// ERROR: "illegal attempt to establish a relationship"
// FIX: Insert objects BEFORE manipulating relationship arrays

// ERROR: "Too many items in %{PROPERTY}@"
// FIX: Check maximumModelCount constraint, reduce array size

// ERROR: "validation recovery attempt FAILED"
// FIX: Non-optional property set to nil via relationship - make optional

// ERROR: Silent save failure
// FIX: Check CloudKit requirements (optional properties, no unique)

// ERROR: Network data not inserting
// FIX: Use MainActor.run for modelContext.insert after URLSession

Quick Reference

Model Definition

import SwiftData

@Model
class User {
    var name: String
    var age: Int
    var email: String?
    
    init(name: String, age: Int, email: String? = nil) {
        self.name = name
        self.age = age
        self.email = email
    }
}

App Setup

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(for: User.self)
    }
}

Query in SwiftUI

struct ContentView: View {
    @Query(sort: \User.name) var users: [User]
    @Environment(\.modelContext) var modelContext
    
    var body: some View {
        List(users) { user in
            Text(user.name)
        }
    }
}

CRUD Operations

// Create
let user = User(name: "Alice", age: 30)
modelContext.insert(user)

// Update
user.name = "Alice Smith"

// Delete
modelContext.delete(user)

// Save (if autosave disabled)
try modelContext.save()

Detailed Guides

Critical Rules

  1. Models must be classes - Structs not supported
  2. @Model required - Apply to all persistent classes
  3. Relationships need care - Use @Relationship for explicit control
  4. CloudKit has strict rules - Optional properties, no unique constraints
  5. Silent failures common - Always verify saves, check constraints
  6. One container per app - Usually sufficient, shared via environment
  7. Context = main actor - Default context is @MainActor safe
  8. Migrations automatic - For simple changes, complex needs MigrationPlan

When to Use Each Pattern

PatternUse WhenAvoid When
Inferred relationshipsSimple optional relationshipsNeed explicit delete rules
@RelationshipNeed delete rules, inverse controlSimple optional relationships
@QuerySwiftUI views, automatic updatesUIKit, manual control needed
FetchDescriptorComplex queries, batch operationsSimple SwiftUI queries
AutosaveMost apps, simple workflowsBatch operations, transactions
Manual saveBatch operations, explicit controlSimple CRUD operations
In-memoryTesting, previews, temporary dataProduction data persistence
CloudKit syncMulti-device, backupLocal-only, complex sharing

Verification Checklist

Before marking task complete:

  • Models have @Model macro
  • App has modelContainer() modifier
  • Relationships have proper delete rules
  • Queries use correct predicate syntax
  • Context is accessed via @Environment
  • CloudKit requirements met (if syncing)
  • Migrations defined (if schema changed)
  • Previews use in-memory containers
  • No silent save failures
  • UI updates when data changes

Resources

  • Apple SwiftData Documentation
  • WWDC23 Session 10187: Meet SwiftData
  • WWDC23 Session 10195: Build an app with SwiftData
  • SwiftData by Example (Paul Hudson)
  • Community articles (see individual section files)

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

xaf-winforms-ui

No summary provided by upstream source.

Repository SourceNeeds Review
General

xaf-office

No summary provided by upstream source.

Repository SourceNeeds Review
General

xaf-editors

No summary provided by upstream source.

Repository SourceNeeds Review