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.
- Author
- TalissonVitorino ↗
- Repository
- kmp-ios-skills ↗
- Licence
- MIT
- Last repository change
- 25.08.2026
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 25.08.2026, without references to retired models or APIs.
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
- Overview
- When to use
- Model
- Container (app entry)
- Query + mutate in a view
- Common mistakes
- See also
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
| Repository | Score | Licence |
|---|---|---|
| This page · TalissonVitorino/kmp-ios-skills | 100 | MIT |
| JordanCoin/ios-skills-collection | 95 | MIT |
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…