From swiftdata-pro
Models, queries, and persists data with SwiftData on iOS 17+. Covers @Model, @Query, ModelContainer, migrations, and CloudKit sync.
How this skill is triggered — by the user, by Claude, or both
Slash command
/swiftdata-pro:swiftdata-proThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Model, query, and persist data with SwiftData correctly and efficiently.
Model, query, and persist data with SwiftData correctly and efficiently.
@Model types and relationships.@Query / fetching in views.ModelContainer, migrations, or CloudKit sync.Trigger: /swiftdata-pro.
@Model classes are reference types managed by a ModelContext..modelContainer(for:).@Query; write through the modelContext.ModelContext for bulk work.@Model
final class Trip {
var name: String
var startDate: Date
@Relationship(deleteRule: .cascade) var stops: [Stop] = []
init(name: String, startDate: Date) {
self.name = name
self.startDate = startDate
}
}
deleteRule on relationships — don't rely on defaults for ownership.@Attribute(.unique) for natural keys.@Attribute(.externalStorage).❌ No delete rule on an owning relationship
var stops: [Stop] = [] // orphans Stop rows when a Trip is deleted
✅
@Relationship(deleteRule: .cascade) var stops: [Stop] = []
@main
struct TripsApp: App {
var body: some Scene {
WindowGroup { ContentView() }
.modelContainer(for: Trip.self)
}
}
Use @Query with sort/filter in the view; don't fetch-all then filter in Swift.
❌
@Query private var trips: [Trip]
var upcoming: [Trip] { trips.filter { $0.startDate > .now } } // loads everything
✅
@Query(filter: #Predicate<Trip> { $0.startDate > Date.now },
sort: \Trip.startDate)
private var upcoming: [Trip]
@Environment(\.modelContext) private var context
func add(_ trip: Trip) {
context.insert(trip)
// SwiftData autosaves; call try? context.save() only when you need it now.
}
Delete with context.delete(trip).
For imports/bulk writes, use a separate context off the main actor and save in batches:
let context = ModelContext(container)
for row in rows { context.insert(Item(row)) }
try context.save()
Don't do thousands of inserts on the main-actor context — it blocks the UI.
SchemaMigrationPlan with versioned schemas and migration
stages. Define VersionedSchema types; never silently mutate a shipped model.@Attribute(.unique) (CloudKit can't enforce it).ModelConfiguration using a CloudKit container identifier.❌ (breaks CloudKit)
@Attribute(.unique) var code: String
✅
var code: String = ""
deleteRule on owning relationships.#Predicate in @Query..unique or non-optional properties on a CloudKit-synced model.Per issue: file:line, rule, before/after. Lead with data-loss / migration risks.
Searches MemPalace before answering questions about past work, people, projects, or prior decisions. Returns verbatim stored content instead of guessing from model memory.
Guides Payload CMS config (payload.config.ts), collections, fields, hooks, access control, APIs. Debugs validation errors, security, relationships, queries, transactions, hook behavior.
Implements vector databases with Pinecone, Weaviate, Qdrant, Milvus, pgvector for semantic search, RAG, recommendations, and similarity systems. Optimizes embeddings, indexing, and hybrid search.
npx claudepluginhub laxrajpurohit/swift-skills-pro --plugin swiftdata-pro