All skills
dpearson2699 avatar

/swiftdata

@cf3fe87

Implement, 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.

Use this Skill: https://skilld.dev/gh/dpearson2699/swift-ios-skills/swiftdata

This session only. Nothing lands on disk.

referencesindexing.md

≈510 tokens on demand. Your agent reads this file only when SKILL.md points to it.

SwiftData Indexing

Indexes speed up queries on frequently filtered or sorted properties.

Docs: Index macro

#Index (iOS 18+)

Single-Property Index

@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:

@Model
class Trip {
    #Index<Trip>([\.destination], [\.startDate])
    // ...
}

This creates two single-column indexes: one on destination and one on startDate.

Compound Index

@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.

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides valid reference documentation and code snippets for working with Apple's SwiftData persistence framework. No malicious actions or security risks were identified.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    1/4 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at cf3fe87. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Steadyupdated 3 months ago

README badge

README badge for dpearson2699/swift-ios-skills/swiftdata