Skip to content
Skills Library
Programming · Building & coding

swiftdata

/swiftdata

Apple SwiftData for iOS 17+ persistence - the @Model macro, ModelContainer and ModelContext, the @Query property wrapper with #Predicate and SortDescriptor, @Relationship with delete rules, @Attribute(.unique), the .modelContainer SwiftUI modifier, and insert/delete/save. Use when persisting data in a native iOS app with SwiftData, building local-first storage, or migrating from Core Data. For the older Core Data API use core-data; for shared KMP persistence used across Android and iOS use sqldelight-patterns; for SwiftUI state around models use swiftui-observation.

Licence
MIT
Last repository change
25.08.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 25.08.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Apple SwiftData for iOS 17+ persistence - the @Model macro, ModelContainer and ModelContext, the @Query property wrapper with #Predicate and SortDescriptor, @Relationship with delete rules, @Attribute(.unique), the .modelContainer SwiftUI modifier, and insert/delete/save. Use when persisting data in a native iOS app with SwiftData, building local-first storage, or migrating from Core Data. For the older Core Data API use core-data; for shared KMP persistence used across Android and iOS use sqldelight-patterns; for SwiftUI state around models use swiftui-observation.

Original author description

Apple SwiftData for iOS 17+ persistence - the @Model macro, ModelContainer and ModelContext, the @Query property wrapper with #Predicate and SortDescriptor, @Relationship with delete rules, @Attribute(.unique), the .modelContainer SwiftUI modifier, and insert/delete/save. Use when persisting data in a native iOS app with SwiftData, building local-first storage, or migrating from Core Data. For the older Core Data API use core-data; for shared KMP persistence used across Android and iOS use sqldelight-patterns; for SwiftUI state around models use swiftui-observation.

How it works

  1. Overview
  2. When to use
  3. Model
  4. Container (app entry)
  5. Query + mutate in a view
  6. Common mistakes
  7. See also
Original SKILL.md headings · 86 lines, 349 words
SKILL.md preview
---
name: swiftdata
description: Apple SwiftData for iOS 17+ persistence - the @Model macro, ModelContainer and ModelContext, the @Query property wrapper with #Predicate and SortDescriptor, @Relationship with delete rules, @Attribute(.unique), the .modelContainer SwiftUI modifier, and insert/delete/save. Use when persisting data in a native iOS app with SwiftData, building local-first storage, or migrating from Core Data. For the older Core Data API use core-data; for shared KMP persistence used across Android and iOS use sqldelight-patterns; for SwiftUI state around models use swiftui-observation.
---

# SwiftData (iOS 17+)

## Overview

SwiftData is Apple's modern persistence framework (iOS 17+), built on Core Data but Swift-native. You declare models with the `@Model` macro; a `ModelContainer` holds the store, a `ModelContext` performs inserts/updates/deletes, and `@Query` drives SwiftUI views reactively.

## When to use

- Local persistence in a native iOS app (iOS 17+)
- SwiftUI-driven, reactive data display
- Migrating from Core Data to a Swift-native API
- NOT for code shared with Android → use `sqldelight-patterns`

## Model

```swift
import SwiftData

@Model
final class Plant {                          // must be a final class, not a struct
    @Attribute(.unique) var id: UUID
    var name: String
    var plantedOn: Date
    @Relationship(deleteRule: .cascade) var waterings: [Watering] = []

    init(name: String, plantedOn: Date = .now) {
        self.id = UUID(); self.name = name; self.plantedOn = plantedOn
    }
}
```

## Container (app entry)

```swift
@main
struct GardenApp: App {
    var body: some Scene {
        WindowGroup { ContentView() }
            .modelContainer(for: Plant.self)   // creates the ModelContainer + store
    }
}
```

## Query + mutate in a view

```swift
struct PlantList: View {
    @Environment(\.modelContext) private var context
    @Query(sort: \Plant.plantedOn, order: .reverse) private var plants: [Plant]

    var body: some View {
        List(plants) { Text($0.name) }
        Button("Add") { context.insert(Plant(name: "Fern")) }   // autosaved
    }
}

// Filtered query with a type-safe predicate
@Query(filter: #Predicate<Plant> { $0.name.contains("Fern") })
private var ferns: [Plant]
```

Explicit delete/save:

```swift
context.delete(plant)
try context.save()        // usually autosaved; call when you need it deterministic
```

## Common mistakes

| Mistake | Fix |
|---|---|
| Forgetting `.modelContainer(...)` | Without it `@Query` / `modelContext` have no store |
| Using `NSPredicate` | Use the type-safe `#Predicate<T> { … }` |
| Heavy writes on the main context | Use a separate `ModelContext` / `@ModelActor` off the main actor |
| `@Model` on a struct | `@Model` requires a `final class` |
| Targeting below iOS 17 | SwiftData is iOS 17+; fall back to `core-data` |

## See also

- Older Apple persistence API: `core-data`
- Shared KMP persistence (Android + iOS): `sqldelight-patterns`
- SwiftUI state around models: `swiftui-observation`
- Background work / actors: `swift-concurrency`

Versions

RepositoryScoreLicence
This page · TalissonVitorino/kmp-ios-skills100MIT
JordanCoin/ios-skills-collection95MIT

Database snapshot: 13.11.2026 · package version bf4877142dd2 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100swiftdata-pro
    Use when modeling data with SwiftData (@Model), writing @Query, configuring ModelContainer, performing migrations, or enabling CloudKit sync on iOS 17+.
  • 90swiftdata-workflow
    Design, implement, migrate, test, and integrate SwiftData persistence in Apple apps using current Apple documentation. Use for @Model schemas, ModelContainer and ModelContext ownership, @Query integration, relationships, uniqueness, deletion, concurrency, migrations, preview or…
  • 100ios-core-data-swiftdata-persistence
    Use this skill when managing local relational persistence, migrations, and CloudKit sync on iOS using SwiftData and Core Data.
  • 100swiftdata-expert-skill
    Use when working with @Model schemas, @Relationship/@Attribute rules, Query or FetchDescriptor data access, ModelContainer/ModelContext configuration, CloudKit sync, SchemaMigrationPlan/history APIs, ModelActor concurrency isolation, or Core Data to SwiftData adoption/coexistence. Expert guidance…
  • 95ios
    Use this skill when the user asks about iOS development, Swift, SwiftUI, UIKit, iOS architecture, MVVM, Coordinator, Core Data, SwiftData, or iOS testing.
  • 100swift-ios
    Use when building native iOS and Apple-platform apps in Swift with SwiftUI and modern Swift concurrency — observable view models, Swift 6 strict data-race diagnostics, async/await and actors, SwiftData persistence, NavigationStack, and Swift Testing. NOT cross-platform Dart…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.