# Swiftdata

Implement, review, or improve data persistence using SwiftData.

- Repository: https://github.com/dpearson2699/swift-ios-skills (1266 stars)
- Category: Mobile
- License: see repository
- Last updated: 2026-07-31
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add dpearson2699/swift-ios-skills --skill swiftdata -g -a claude-code -y`
Codex: `npx skills add dpearson2699/swift-ios-skills --skill swiftdata -g -a codex -y`
Cursor: `npx skills add dpearson2699/swift-ios-skills --skill swiftdata -g -a cursor -y`
Gemini CLI: `npx skills add dpearson2699/swift-ios-skills --skill swiftdata -g -a gemini-cli -y`
Claude Code plugin marketplace: `/plugin marketplace add dpearson2699/swift-ios-skills`

## Description

Implement, review, or improve data persistence using SwiftData. Use when defining @Model classes with @Attribute, @Relationship, @Transient, #Unique, or #Index; when querying with @Query, #Predicate, FetchDescriptor, or SortDescriptor; when configuring ModelContainer and ModelContext for SwiftUI or background work with @ModelActor; when planning schema migrations with VersionedSchema and SchemaMigrationPlan; when setting up CloudKit sync with ModelConfiguration; or when coexisting with or migrating from Core Data.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# SwiftData

Persist, query, and manage structured data in iOS 26+ apps using SwiftData
with Swift 6.3.

## Contents

- [Model Definition](#model-definition)
- [ModelContainer Setup](#modelcontainer-setup)
- [CloudKit Sync](#cloudkit-sync)
- [CRUD Operations](#crud-operations)
- [`@Query in SwiftUI`](#query-in-swiftui)
- [#Predicate](#predicate)
- [FetchDescriptor](#fetchdescriptor)
- [Schema Versioning and Migration](#schema-versioning-and-migration)
- [Core Data Coexistence Boundary](#core-data-coexistence-boundary)
- [Concurrency (`@ModelActor`)](#concurrency-modelactor)
- [SwiftUI Integration](#swiftui-integration)
- [Common Mistakes](#common-mistakes)
- [Review Checklist](#review-checklist)
- [References](#references)

## Model Definition

Apply `@Model` to a **class** (not struct). It synthesizes `PersistentModel`
conformance. Model instances remain context/actor-bound; pass their
`PersistentIdentifier`, not the instance, across actors.

```swift
@Model
class Trip {
    var name: String
    var destination: String
    var startDate: Date
    var endDate: Date
    var isFavorite: Bool = false
    @Attribute(.externalStorage) var imageData: Data?
    @Relationship(deleteRule: .cascade, inverse: \LivingAccommodation.trip)
    var accommodation: LivingAccommodation?
    @Transient var isSelected: Bool = false  // Always provide default

    init(name: String, destination: String, startDate: Date, endDate: Date) {
        self.name = name; self.destination = destination
        self.startDate = startDate; self.endDate = endDate
    }
}
```

**`@Attribute` options**: `.externalStorage`, `.unique`, `.spotlight`, `.allowsCloudEncryption`, `.preserveValueOnDeletion`, `.ephemeral`, `.transformable(by:)`. Rename: `@Attribute(originalName: "old_name")`.

**`@Relationship`**: `deleteRule:` `.cascade`/`.nullify`(default)/`.deny`/`.noAction`. Specify `inverse:` for reliable behavior. Unidirectional (iOS 18+): `inverse: nil`.

The rest: https://github.com/dpearson2699/swift-ios-skills/blob/HEAD/skills/swiftdata/SKILL.md
