swift-data
This skill equips AI agents with the knowledge to architect scalable data persistence solutions in Swift using SwiftData's modern ORM capabilities. It covers entity modeling, repository patterns, and clean architecture principles tailored for MVVM-C workflows. Developers gain practical guidance on structuring data layers that remain maintainable as iOS projects grow in complexity.
swift-data enables modular MVVM-C architectures by separating concerns into distinct layers. Create a repository pattern that abstracts SwiftData's ModelContainer and @Query operations behind protocol-based interfaces. Define domain models independently from @Model entities, then map between them in a dedicated mapper layer. This isolation lets ViewModels depend only on repository protocols, not SwiftData directly, making testing and feature scaling straightforward.
AI-generated summary based on this skill's SKILL.md
Install
pproenca/dot-skills/swift-data · repository language: Shell
git clone https://github.com/pproenca/dot-skills
cp -r dot-skills/skills/.experimental/swift-data ~/.claude/skills/swift-dataFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How do you build a modular MVVM-C data layer with SwiftData?
swift-data enables modular MVVM-C architectures by separating concerns into distinct layers. Create a repository pattern that abstracts SwiftData's ModelContainer and @Query operations behind protocol-based interfaces. Define domain models independently from @Model entities, then map between them in a dedicated mapper layer. This isolation lets ViewModels depend only on repository protocols, not SwiftData directly, making testing and feature scaling straightforward.
What is SwiftData stale-while-revalidate and how do you implement it?
swift-data supports stale-while-revalidate reads by serving cached data immediately while fetching fresh results in the background. Implement this by storing a timestamp with each entity, querying local data first with @Query, then triggering a background sync task that updates the ModelContainer when new data arrives. Use @ObservedReferenceable or manual refresh triggers to notify SwiftUI views of updates without blocking the initial render.
How does SwiftData handle optimistic writes and queued operations?
swift-data optimistic writes queue changes locally before server confirmation. Save mutations to the ModelContainer immediately, assign a pending state flag, and enqueue the write operation. If the network request succeeds, clear the flag; on failure, either retry automatically or present a conflict resolution UI. This pattern keeps the app responsive while maintaining eventual consistency with your backend.
What are best practices for SwiftData schema migration and relationships?
swift-data schema migrations use versioned ModelContainer configurations. Define one-to-many relationships with @Relationship(deleteRule: .cascade) to maintain referential integrity. Version your schema incrementally, providing migration logic in ModelConfiguration. For complex relationships, use lightweight migrations when possible. Test migrations on sample data before release to catch cascading delete issues and orphaned records early.
How do you set up SwiftData ModelContainer and error recovery?
swift-data ModelContainer initialization requires specifying your @Model types and a ModelConfiguration. Wrap container creation in try-catch to handle initialization failures gracefully. Implement error recovery by logging failures, offering users options to retry or clear local data, and maintaining a fallback in-memory store if persistence fails. Set up preview infrastructure with lightweight ModelContainers using inMemory storage for SwiftUI previews and tests.
How does SwiftData support offline-first persistence and sync?
swift-data enables offline-first architectures by persisting all writes locally first. Implement background sync tasks that queue pending changes, retry failed syncs with exponential backoff, and resolve conflicts when offline edits clash with server state. Use @Query to surface sync status in UI, letting users see which changes are pending. Combine with URLSession background tasks for reliable sync even when the app is backgrounded.
SKILL.md
rendered from the published skill — quoted content, verbatim
SwiftData Best Practices — Modular MVVM-C Data Layer
Comprehensive data modeling, persistence, sync architecture, and error handling guide for SwiftData aligned with the clinic modular MVVM-C stack.
Architecture Alignment
This skill enforces the same modular architecture mandated by swift-ui-architect:
``` ┌───────────────────────────────────────────────────────────────┐ │ Feature modules: View + ViewModel, no SwiftData imports │ ├───────────────────────────────────────────────────────────────┤ │ Domain: models + repository/coordinator/error protocols │ ├───────────────────────────────────────────────────────────────┤ │ Data: @Model entities, SwiftData stores, repository impls, │ │ remote clients, retry executor, sync queue, conflict handling
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 15 files
skills/.experimental/swift-data/SKILL.md
skills/.experimental/swift-data/metadata.json
skills/.experimental/swift-data/references/_sections.md
skills/.experimental/swift-data/references/crud-cancel-delete.md
skills/.experimental/swift-data/references/crud-delete-indexset.md
skills/.experimental/swift-data/references/crud-dismiss-save.md
skills/.experimental/swift-data/references/crud-edit-button.md
skills/.experimental/swift-data/references/crud-insert-context.md
skills/.experimental/swift-data/references/crud-save-error-handling.md
skills/.experimental/swift-data/references/crud-sheet-creation.md
skills/.experimental/swift-data/references/crud-undo-cancel.md
skills/.experimental/swift-data/references/model-class-for-persistence.md
skills/.experimental/swift-data/references/model-computed-properties.md
skills/.experimental/swift-data/references/model-custom-types.md
skills/.experimental/swift-data/references/model-defaults.md