swift-data

📁 pproenca/dot-skills 📅 3 days ago
28
总安装量
3
周安装量
#13204
全站排名
安装命令
npx skills add https://github.com/pproenca/dot-skills --skill swift-data

Agent 安装分布

claude-code 3
amp 2
opencode 2
kimi-cli 2
github-copilot 2

Skill 文档

Apple Developer SwiftData Best Practices

Comprehensive data modeling, persistence, and state management guide for Swift and SwiftUI applications using SwiftData, sourced from official Apple Developer tutorials and WWDC sessions. Contains 48 rules across 8 categories (after merging duplicates and adding concurrency coverage), prioritized by impact to guide automated refactoring and code generation.

When to Apply

Reference these guidelines when:

  • Defining @Model classes and their properties
  • Setting up ModelContainer and ModelContext for persistence
  • Writing @Query declarations and predicates
  • Implementing create, update, and delete operations
  • Configuring model relationships (one-to-many, inverse)
  • Coordinating SwiftUI state with SwiftData (@Bindable, @State, @Environment)
  • Building preview infrastructure with sample data
  • Planning schema migrations for app updates

Workflow

Use this workflow when designing or refactoring a SwiftData-backed feature:

  1. Model design: define @Model classes, defaults, and transient/computed state (see model-*)
  2. Container wiring: configure ModelContainer once at the app boundary; choose default vs custom configuration; decide App Group sharing (see persist-container-setup, schema-configuration, persist-app-group)
  3. Queries: prefer @Query in views; use FetchDescriptor in services/background work (see query-property-wrapper, query-fetch-descriptor, query-fetch-tuning)
  4. CRUD flows: insert/delete via the environment context; choose creation UI patterns; handle cancel/undo appropriately (see crud-*)
  5. Relationships: model to-many relationships as arrays; define delete rules for ownership (see rel-*)
  6. Previews: create in-memory containers and sample data for fast iteration (see preview-*)
  7. Schema evolution: plan migrations and validate uniqueness/indexing choices before shipping (see schema-*)

Troubleshooting

  • Data not persisting -> persist-model-macro, persist-container-setup, persist-autosave, schema-configuration
  • List not updating -> query-property-wrapper, state-wrapper-views
  • Duplicates -> schema-unique-attributes, schema-unique-macro
  • Widget/extension can’t see data -> persist-app-group, schema-configuration

Rule Categories by Priority

Priority Category Impact Prefix
1 Data Modeling CRITICAL model-
2 Persistence Setup CRITICAL persist-
3 Querying & Filtering HIGH query-
4 CRUD Operations HIGH crud-
5 Relationships MEDIUM-HIGH rel-
6 SwiftUI State Flow MEDIUM state-
7 Sample Data & Previews MEDIUM preview-
8 Schema & Migration LOW-MEDIUM schema-

Quick Reference

1. Data Modeling (CRITICAL)

2. Persistence Setup (CRITICAL)

3. Querying & Filtering (HIGH)

4. CRUD Operations (HIGH)

5. Relationships (MEDIUM-HIGH)

6. SwiftUI State Flow (MEDIUM)

7. Sample Data & Previews (MEDIUM)

8. Schema & Migration (LOW-MEDIUM)

How to Use

Read individual reference files for detailed explanations and code examples:

Reference Files

File Description
references/_sections.md Category definitions and ordering
assets/templates/_template.md Template for new rules
metadata.json Version and reference information