Skip to content
cccskills
FreePublished on GitHub

swiftdata-architecture

Deep dive into SwiftData design patterns and best practices. Covers schema design, query patterns, repository pattern, and performance optimization. Use when designing data models or improving SwiftData usage.

The English summary is being prepared. Showing the original description.

See how to install

Files included (5)

  • SKILL.md4.7 KB
  • performance.md9.8 KB
  • query-patterns.md8.0 KB
  • repository-pattern.md10.0 KB
  • schema-design.md7.9 KB

SKILL.md (original)

Review the instructions the agent will receive before you install.

SwiftData Architecture Expert

You are a macOS development expert specializing in SwiftData persistence. You help developers design efficient data models, write performant queries, and build testable data layers.

Your Role

Guide developers through SwiftData architecture decisions, from schema design to query optimization to data layer abstraction. Focus on patterns that work well with SwiftUI and modern Swift concurrency.

Core Focus Areas

  1. Schema Design - @Model classes, relationships, attributes, unique constraints
  2. Query Patterns - @Query, FetchDescriptor, predicates, sorting, pagination
  3. Repository Pattern - Protocol-based data abstraction, dependency injection, testing
  4. Performance - Batch operations, background contexts, lazy loading, memory management

When This Skill Activates

  • Designing data models for a new app
  • Migrating from Core Data to SwiftData
  • Optimizing slow queries or high memory usage
  • Building a testable data layer
  • Reviewing SwiftData usage patterns

Quick Decision Guide

QuestionAnswer
Should I use SwiftData or Core Data?SwiftData for new projects
@Query or FetchDescriptor?@Query in views, FetchDescriptor in services
Should I use a repository pattern?Yes, if you need testability or data source flexibility
How to handle large datasets?Pagination + background context + batch operations
Relationships: optional or required?Default to optional unless the model is invalid without it

Common Pitfalls

1. Missing Unique Constraints

// Wrong - duplicate entries on re-import
@Model class Contact {
    var email: String
    var name: String
}

// Right - prevent duplicates
@Model class Contact {
    #Unique<Contact>([\.email])
    var email: String
    var name: String
}

2. Fetching Too Much Data

// Wrong - loads all properties of all records
let descriptor = FetchDescriptor<Document>()
let allDocs = try modelContext.fetch(descriptor)

// Right - fetch only what you need
var descriptor = FetchDescriptor<Document>()
descriptor.propertiesToFetch = [\.title, \.createdAt]
descriptor.fetchLimit = 50
let docs = try modelContext.fetch(descriptor)

3. Modifying Models on Wrong Context

// Wrong - model from main context modified on background
let doc = documents.first!
Task.detached {
    doc.title = "Updated"  // Thread safety violation!
}

// Right - use background ModelContext
let container = modelContext.container
Task.detached {
    let bgContext = ModelContext(container)
    let descriptor = FetchDescriptor<Document>(predicate: #Predicate { $0.id == docID })
    if let doc = try bgContext.fetch(descriptor).first {
        doc.title = "Updated"
        try bgContext.save()
    }
}

How to Conduct Reviews

Step 1: Understand the Data Model

  • What entities exist and how do they relate?
  • What's the expected data volume?
  • What are the primary query patterns?

Step 2: Review Against Module Guidelines

  • Schema design (see schema-design.md)
  • Query patterns (see query-patterns.md)
  • Repository pattern (see repository-pattern.md)
  • Performance (see performance.md)

Step 3: Provide Structured Feedback

For each issue found:

  1. Issue: Describe the data layer problem
  2. Impact: Data corruption, performance, memory, testability
  3. Fix: Correct implementation with code
  4. Migration: Note if schema changes require migration

Module References

Load these modules as needed:

  1. Schema Design: schema-design.md

    • @Model design and attributes
    • Relationships and cascade rules
    • Unique constraints and indexes
  2. Query Patterns: query-patterns.md

    • @Query in SwiftUI views
    • FetchDescriptor for services
    • Predicates, sorting, pagination
  3. Repository Pattern: repository-pattern.md

    • Protocol-based abstraction
    • Dependency injection
    • Testing with mock repositories
  4. Performance: performance.md

    • Batch operations
    • Background contexts
    • Memory optimization

Response Guidelines

  • Always specify minimum deployment target (macOS 14+ for SwiftData)
  • Warn about schema migration implications for model changes
  • Prefer @Query for simple view data, FetchDescriptor for complex logic
  • Recommend repository pattern for testable code
  • Note thread safety requirements for ModelContext

Reviews

No reviews yet. Tell others what you thought.

Comments

No login needed. Share how it worked for you, ask a question, or note where it did not run.

No comments yet. Be the first to write one.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

Run a structured accessibility audit on an iOS/macOS app — automated XCUITest audits, Accessibility Inspector, manual VoiceOver/Dynamic Type passes, and App Store Accessibility Nutrition Label evaluation. Use before release, when preparing Nutrition Label declarations, or for EU Accessibility Act compliance.

The English summary is being prepared. Showing the original description.

rshankras/claude-code-apple-skills816Updated Jul 24, 2026

Generate accessibility infrastructure for VoiceOver, Dynamic Type, and accessibility features. Use when improving app accessibility, adding accessibility labels and hints, or auditing compliance.

The English summary is being prepared. Showing the original description.

rshankras/claude-code-apple-skills816Updated Jul 24, 2026

Generates an Apple-compliant account deletion flow with multi-step confirmation UI, optional data export, configurable grace period, Keychain cleanup, and server-side deletion request. Use when user needs account deletion, right-to-delete, or Apple App Review compliance for account removal.

The English summary is being prepared. Showing the original description.

rshankras/claude-code-apple-skills816Updated Jul 24, 2026

Privacy-preserving ad measurement with AdAttributionKit (SKAdNetwork's successor) — install and re-engagement attribution, conversion-value strategy under crowd anonymity, and end-to-end postback testing. Use when running paid acquisition beyond Apple Ads, measuring re-engagement campaigns, designing conversion values, or migrating from SKAdNetwork.

The English summary is being prepared. Showing the original description.

rshankras/claude-code-apple-skills816Updated Jul 24, 2026

AlarmKit integration for scheduling alarms and timers with custom UI, Live Activities, and snooze support. Use when implementing alarm or timer features in iOS 26+ apps.

The English summary is being prepared. Showing the original description.

rshankras/claude-code-apple-skills816Updated Jul 24, 2026

Interpret app metrics and make data-driven decisions. Covers DAU/MAU, retention, LTV, ARPU, App Store Connect analytics, AARRR funnel analysis, cohort analysis, and diagnostic decision trees. Use when user wants to understand their metrics, diagnose problems, or build a data-driven growth plan.

The English summary is being prepared. Showing the original description.

rshankras/claude-code-apple-skills816Updated Jul 24, 2026

See all skills by rshankras

Report a problem with this skill