Install any skill in seconds. Free to start, no credit card required.
Get Started Free →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.
.claude/skills/rshankras-swiftdata-architecture/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | -27% | 0% |
| case-02 | ✗→✓ | ▲ Improved | -1% | 0% |
| case-04 | ✗→✓ | ▲ Improved | 49% | 0% |
| case-05 | ✗→✓ | ▲ Improved | 27% | 0% |
| case-12 | ✗→✓ | ▲ Improved | 47% | 0% |
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 new projects | | @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 |
swift// 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 }
swift// 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)
swift// 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.mdquery-patterns.mdrepository-pattern.mdperformance.mdOther measured skills in the registry, with their headline benchmark lift.