Install
$ agentstack add skill-onmyway133-skills-swift-data ✓ scanned · ✓ verified — works with Claude Code, Cursor, and more.
Security review
✓ PassedNo issues found. Passed automated security review. · v0.1.0 How review works →
- ✓ Prompt-injection patterns
- ✓ Secret / credential exfiltration
- ✓ Dangerous shell & filesystem operations
- ✓ Untrusted network calls
- ✓ Known-malicious package signatures
What it can access
- ● Network access Used
- ✓ Filesystem access No
- ✓ Shell / process execution No
- ✓ Environment & secrets No
- ✓ Dynamic code execution No
From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.
About
SwiftData
You are a SwiftData expert. Apply these patterns when working with data persistence in Swift apps.
When to Use
- Adding persistence to SwiftUI apps
- Defining data models with relationships
- Querying and filtering data
- Syncing with iCloud/CloudKit
- Migrating from Core Data
Decision Tree
| Need | Reference | |------|-----------| | Define models and properties | → [model-basics.md](references/model-basics.md) | | Create relationships between models | → [relationships.md](references/relationships.md) | | Query, filter, and sort data | → [queries-filtering.md](references/queries-filtering.md) | | Configure containers and contexts | → [containers-contexts.md](references/containers-contexts.md) | | Enable iCloud sync | → [cloudkit-sync.md](references/cloudkit-sync.md) | | Handle schema migrations | → [migration.md](references/migration.md) | | Optimize performance | → [performance.md](references/performance.md) | | Debug and troubleshoot | → [common-issues.md](references/common-issues.md) |
Core Principles
- Use @Model for all persistent types - Classes only, not structs
- Relationships must be optional or have defaults - For CloudKit compatibility
- ModelContext is thread-bound - Never pass contexts between threads
- Prefer @Query in SwiftUI - Automatic updates when data changes
- Autosave is on by default - Changes persist automatically
Quick Reference
Basic Model
import SwiftData
@Model
final class Book {
var title: String
var author: String
var publishedDate: Date
var rating: Int?
init(title: String, author: String, publishedDate: Date) {
self.title = title
self.author = author
self.publishedDate = publishedDate
}
}
App Setup
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: Book.self)
}
}
Query in SwiftUI
struct BookList: View {
@Query(sort: \Book.title) var books: [Book]
@Environment(\.modelContext) var modelContext
var body: some View {
List(books) { book in
Text(book.title)
}
}
}
CRUD Operations
// Create
let book = Book(title: "Swift Guide", author: "Apple", publishedDate: .now)
modelContext.insert(book)
// Read - use @Query or fetch
let descriptor = FetchDescriptor(
predicate: #Predicate { $0.rating ?? 0 > 3 },
sortBy: [SortDescriptor(\.title)]
)
let books = try modelContext.fetch(descriptor)
// Update - modify properties directly
book.title = "Updated Title"
// Delete
modelContext.delete(book)
Note
For comprehensive SwiftData tutorials, check Hacking with Swift - SwiftData.
References
- [model-basics.md](references/model-basics.md) - @Model macro, properties, attributes
- [relationships.md](references/relationships.md) - One-to-one, one-to-many, many-to-many
- [queries-filtering.md](references/queries-filtering.md) - @Query, predicates, sorting
- [containers-contexts.md](references/containers-contexts.md) - ModelContainer, ModelContext, configuration
- [cloudkit-sync.md](references/cloudkit-sync.md) - iCloud sync setup and constraints
- [migration.md](references/migration.md) - Lightweight and complex migrations
- [performance.md](references/performance.md) - Batch operations, background contexts
- [common-issues.md](references/common-issues.md) - Debugging and troubleshooting
Source & license
This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.
- Author: onmyway133
- Source: onmyway133/skills
- License: MIT
- Homepage: https://onmyway133.com
Install and usage instructions live in the source repository linked above.
Reviews
No reviews yet — be the first.
Write a review
Versions
- v0.1.0 Imported from the upstream source.