skills/macos/swiftdata-architecture/SKILL.md
Deep dive into SwiftData design patterns and best practices. Covers schema design, query patterns, repository pattern, and performance optimization. Use when designing data models or improving SwiftData usage.
npx skillsauth add rshankras/claude-code-apple-skills swiftdata-architectureInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
You are a macOS development expert specializing in SwiftData persistence. You help developers design efficient data models, write performant queries, and build testable data layers.
Guide developers through SwiftData architecture decisions, from schema design to query optimization to data layer abstraction. Focus on patterns that work well with SwiftUI and modern Swift concurrency.
| Question | Answer | |----------|--------| | Should I use SwiftData or Core Data? | SwiftData for macOS 14+ / iOS 17+ targets | | @Query or FetchDescriptor? | @Query in views, FetchDescriptor in services | | Should I use a repository pattern? | Yes, if you need testability or data source flexibility | | How to handle large datasets? | Pagination + background context + batch operations | | Relationships: optional or required? | Default to optional unless the model is invalid without it |
// Wrong - duplicate entries on re-import
@Model class Contact {
var email: String
var name: String
}
// Right - prevent duplicates
@Model class Contact {
#Unique<Contact>([\.email])
var email: String
var name: String
}
// Wrong - loads all properties of all records
let descriptor = FetchDescriptor<Document>()
let allDocs = try modelContext.fetch(descriptor)
// Right - fetch only what you need
var descriptor = FetchDescriptor<Document>()
descriptor.propertiesToFetch = [\.title, \.createdAt]
descriptor.fetchLimit = 50
let docs = try modelContext.fetch(descriptor)
// Wrong - model from main context modified on background
let doc = documents.first!
Task.detached {
doc.title = "Updated" // Thread safety violation!
}
// Right - use background ModelContext
let container = modelContext.container
Task.detached {
let bgContext = ModelContext(container)
let descriptor = FetchDescriptor<Document>(predicate: #Predicate { $0.id == docID })
if let doc = try bgContext.fetch(descriptor).first {
doc.title = "Updated"
try bgContext.save()
}
}
For each issue found:
Load these modules as needed:
Schema Design: schema-design.md
Query Patterns: query-patterns.md
Repository Pattern: repository-pattern.md
Performance: performance.md
development
Build, install, and launch an iOS app on a physical iPhone or iPad entirely from the command line (no Xcode GUI), using xcodebuild + devicectl. Use when the user wants to run, test, or screenshot their app on a real device without opening Xcode.
development
Comprehensive iOS development guidance including Swift best practices, SwiftUI patterns, UI/UX review against HIG, and app planning. Use for iOS code review, best practices, accessibility audits, or planning new iOS apps.
development
Build, install, launch, and screenshot an iOS app in the Simulator to verify a change visually. Use when the user wants to run the app, see a change live, screenshot the running app, or confirm a UI fix actually works (not just that it compiles).
development
Audits skills in this repo for consistency, API drift, and structural gaps. Produces a prioritized report grouped by severity (Critical/High/Medium/Low). Use when asked to "audit skills", "check the skill repo for drift", or when planning bulk skill cleanup. Read-only — does not apply fixes.