Skip to content
Back to skills

Swiftdata

ASecurity

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 sq...

  • 16 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added November 11, 2026
developmentswiftsqlreactapi

Works with

  • api

Security analysis

A100/100

Scanned November 11, 2026

npx -y skills add TalissonVitorino/kmp-ios-skills --skill swiftdata --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Swiftdata?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Swiftdata
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/talissonvitorino-swiftdata/badge)](https://www.skillsdirectory.com/skills/talissonvitorino-swiftdata)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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`

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.