swiftdata-architecture

Guides SwiftData schema design, query patterns, repository abstraction, and performance optimization for Apple platforms.

Updated Mar 12, 2026
One-click install
npx skills add https://github.com/RavitejaKarra24/dotfiles --skill swiftdata-architecture-ravitejakarra24
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: swiftdata-architecture
Source: https://github.com/RavitejaKarra24/dotfiles/tree/main/agents/.agents/skills/macos-development/swiftdata-architecture
Command: npx skills add https://github.com/RavitejaKarra24/dotfiles --skill swiftdata-architecture-ravitejakarra24

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Designing a SwiftData persistence layer involves many decisions—schema modeling, query construction, thread safety, and testability—where mistakes lead to duplicate records, slow fetches, memory bloat, and untestable code. This Skill provides structured guidance and reviewed patterns for building correct, performant SwiftData data layers. ## Core Features & Use Cases - Schema Design Review: Covers @Model classes, relationships, cascade delete rules, unique constraints, and schema migration with VersionedSchema. - Query Pattern Guidance: Explains when to use @Query versus FetchDescriptor, predicate construction, pagination, and fetch limits. - Repository Pattern & Testing: Shows protocol-based data abstraction with DTOs, @ModelActor implementations, and in-memory repositories for unit tests. - Performance Optimization: Details batch operations, background contexts, memory management, and predicate performance. - Use Case: When migrating an app from Core Data to SwiftData, use this Skill to review the new schema for missing unique constraints, move heavy imports into a @ModelActor, and wrap data access in a testable repository. ## Quick Start Ask the assistant to review your SwiftData data layer or design a schema for your app's models, mentioning your entities and query needs.

Frequently Asked Questions about swiftdata-architecture

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I design a SwiftData schema with relationships?

Define @Model classes with relationship properties and choose delete rules: cascade for owned children, nullify for independent children, deny when children must be handled first. Add unique constraints with #Unique for natural keys like email to prevent duplicates.

Should I use @Query or FetchDescriptor in SwiftData?

Use @Query in SwiftUI views because it auto-updates when data changes. Use FetchDescriptor in ViewModels and services where you need pagination, fetchCount, identifier-only fetches, or background context execution.

SwiftData vs Core Data: which should I use for a new app?

SwiftData is recommended for new projects targeting macOS 14 or later, offering a Swift-native API with @Model macros and SwiftUI integration. Core Data remains relevant for older deployment targets or existing codebases.

How do I make SwiftData code testable with the repository pattern?

Define a repository protocol returning DTOs instead of @Model types, implement it with a @ModelActor for SwiftData, and provide an in-memory actor implementation for tests. Inject the repository into ViewModels via initializer or SwiftUI environment.

Why is my SwiftData fetch slow or using too much memory?

Common causes are fetching all records without fetchLimit, materializing objects just to count them, and running heavy work on the main context. Use fetchCount, propertiesToFetch, pagination, and @ModelActor for background batch operations.

Can I modify SwiftData models on a background thread?

Not directly—models are bound to their ModelContext. Create a background ModelContext from the same container or use @ModelActor, re-fetch the model by identifier in that context, then modify and save there.