dpearson2699/swift-ios-skills / skills/swiftdata
swiftdata
VerifiedImplement, review, or improve data persistence using SwiftData. Use when defining @Model classes with @Attribute, @Relationship, @Transient, #Unique, or #Index; when querying with @Query, #Predicate, FetchDescriptor, or SortDescriptor; when configuring ModelContainer and ModelContext for SwiftUI or background work with @ModelActor; when planning schema migrations with VersionedSchema and SchemaMigrationPlan; when setting up CloudKit sync with ModelConfiguration; or when coexisting with or migrating from Core Data.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 15.4 KB |
| evals/evals.json | config | 3.8 KB |
| references/core-data-coexistence.md | markdown | 10.7 KB |
| references/indexing.md | markdown | 2.0 KB |
| references/predicate-pitfalls.md | markdown | 2.7 KB |
| references/swiftdata-advanced.md | markdown | 24.2 KB |
| references/swiftdata-queries.md | markdown | 18.0 KB |
references/indexing.md
View on GitHub# SwiftData Indexing
Indexes speed up queries on frequently filtered or sorted properties.
Docs: [Index macro](https://sosumi.ai/documentation/swiftdata/index(_:)-74ia2)
## #Index (iOS 18+)
### Single-Property Index
```swift
@Model
class Trip {
@Attribute(.unique) var id: UUID
var destination: String
var startDate: Date
var isFavorite: Bool
init(destination: String, startDate: Date) {
self.id = UUID()
self.destination = destination
self.startDate = startDate
self.isFavorite = false
}
}
```
Use `#Index` on the model to declare indexes:
```swift
@Model
class Trip {
#Index<Trip>([\.destination], [\.startDate])
// ...
}
```
This creates two single-column indexes: one on `destination` and one on `startDate`.
### Compound Index
```swift
@Model
class Trip {
#Index<Trip>([\.isFavorite, \.startDate])
// ...
}
```
A compound index on `(isFavorite, startDate)` accelerates queries that filter on `isFavorite` and sort by `startDate`.
## When to Index
**Index when:**
- A property appears in `#Predicate` filters on large collections (1000+ rows)
- A property is used in `SortDescriptor` on large collections
**Don't index when:**
- The collection is small (< few hundred rows)
- The property is rarely queried
- The property changes very frequently (index maintenance cost)
- The property has very low cardinality (e.g., a Boolean on a small table)
- Another schema constraint or existing index already covers the query pattern,
unless profiling shows a separate index helps
## Index and Migration
Treat adding or removing an index as a schema change. Test it on representative
stores and keep a `VersionedSchema` / `SchemaMigrationPlan` ready for any
release where the aggregate schema changes exceed SwiftData's automatic
migration support.
## Verifying Index Usage
Use Instruments > Core Data template to verify that queries use indexes. Look for "Full Table Scan" warnings on frequently executed fetches — these indicate a missing index.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| planning-and-task-breakdownaddyosmani/agent-skillsBreaks work into ordered tasks. Use when you have a spec or clear requirements and need to break work into implementable tasks. Use when a task feels too large to start, when you need to estimate scope, or when parallel work is possible. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| screenpipethedotmack/claude-memSearch screen and audio history recorded by Screenpipe and optionally save selected context into claude-mem. Use when the user asks to recall desktop activity, a meeting, or something seen in another app through Screenpipe. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |
| contact-widgetnexu-io/open-designSelf-contained floating chat widget with welcome screen, social links, meeting button, and message input. Single HTML file, zero dependencies. | Stars: 108k | Updated | Claude CodeCodex | Verified | Install |
| meeting-notesnexu-io/open-designMeeting notes page — title bar with attendees, agenda checklist, decisions block, action items table with owners + dates, and a "next meeting" footer. Use when the brief mentions "meeting notes", "minutes", "1:1 notes", "all-hands recap", or "会议纪要". | Stars: 108k | Updated | Claude CodeCodex | Verified | Install |