$npx -y skills add AvdLee/Core-Data-Agent-Skill --skill core-data-expertExpert Core Data guidance (iOS/macOS): stack setup, fetch requests & NSFetchedResultsController, saving/merge conflicts, threading & Swift Concurrency, batch operations & persistent history, migrations, performance, and NSPersistentCloudKitContainer/CloudKit sync.
| 1 | # Core Data Expert |
| 2 | |
| 3 | Fast, production-oriented guidance for building **correct**, **performant** Core Data stacks and fixing common crashes. |
| 4 | |
| 5 | ## Agent behavior contract (follow these rules) |
| 6 | |
| 7 | 1. Determine OS/deployment target when advice depends on availability (iOS 14+/17+ features, etc.). |
| 8 | 2. Identify the context type before proposing fixes: **view context (UI)** vs **background context (heavy work)**. |
| 9 | 3. Recommend `NSManagedObjectID` for cross-context/cross-task communication; **never pass `NSManagedObject` instances** across contexts. |
| 10 | 4. Prefer lightweight migration when possible; use staged migration (iOS 17+) for complex changes. |
| 11 | 5. When recommending batch operations, verify persistent history tracking is enabled (often required for UI updates). |
| 12 | 6. For CloudKit integration, remind developers that **Production schema is immutable**. |
| 13 | 7. Reference WWDC/external resources sparingly; prefer this skill’s `references/`. |
| 14 | |
| 15 | ## First 60 seconds (triage template) |
| 16 | |
| 17 | - **Clarify the goal**: setup, bugfix, migration, performance, CloudKit? |
| 18 | - **Collect minimal facts**: |
| 19 | - platform + deployment target |
| 20 | - store type (SQLite / in-memory) and whether CloudKit is enabled |
| 21 | - context involved (view vs background) and whether Swift Concurrency is in use |
| 22 | - exact error message + stack trace/logs |
| 23 | - **Branch immediately**: |
| 24 | - threading/crash → focus on context confinement + `NSManagedObjectID` handoff |
| 25 | - migration error → identify model versions + migration strategy |
| 26 | - batch ops not updating UI → persistent history tracking + merge pipeline |
| 27 | |
| 28 | ## Routing map (pick the right reference fast) |
| 29 | |
| 30 | - **Stack setup / merge policies / contexts** → `references/stack-setup.md` |
| 31 | - **Saving patterns** → `references/saving.md` |
| 32 | - **Fetch requests / list updates / aggregates** → `references/fetch-requests.md` |
| 33 | - **Traditional threading (perform/performAndWait, object IDs)** → `references/threading.md` |
| 34 | - **Swift Concurrency (async/await, actors, Sendable, DAOs)** → `references/concurrency.md` |
| 35 | - **Batch insert/delete/update** → `references/batch-operations.md` |
| 36 | - **Persistent history tracking + “batch ops not updating UI”** → `references/persistent-history.md` |
| 37 | - **Model configuration (constraints, validation, derived/composite, transformables)** → `references/model-configuration.md` |
| 38 | - **Schema migration (lightweight/staged/deferred)** → `references/migration.md` |
| 39 | - **CloudKit integration & debugging** → `references/cloudkit-integration.md` |
| 40 | - **Performance profiling & memory** → `references/performance.md` |
| 41 | - **Testing patterns** → `references/testing.md` |
| 42 | - **Terminology** → `references/glossary.md` |
| 43 | |
| 44 | ## Common errors → next best move |
| 45 | |
| 46 | - **“Failed to find a unique match for an NSEntityDescription”** → `references/testing.md` (shared `NSManagedObjectModel`) |
| 47 | - **`NSPersistentStoreIncompatibleVersionHashError`** → `references/migration.md` (versioning + migration) |
| 48 | - **Cross-context/threading exceptions** (e.g. delete/update from wrong context) → `references/threading.md` and/or `references/concurrency.md` (use `NSManagedObjectID`) |
| 49 | - **Sendable / actor-isolation warnings around Core Data** → `references/concurrency.md` (don’t “paper over” with `@unchecked Sendable`) |
| 50 | - **`NSMergeConflict` / constraint violations** → `references/model-configuration.md` + `references/stack-setup.md` (constraints + merge policy) |
| 51 | - **Batch operations not updating UI** → `references/persistent-history.md` + `references/batch-operations.md` |
| 52 | - **CloudKit schema/sync issues** → `references/cloudkit-integration.md` |
| 53 | - **Memory grows during fetch** → `references/performance.md` + `references/fetch-requests.md` |
| 54 | |
| 55 | ## Verification checklist (when changing Core Data code) |
| 56 | |
| 57 | - Confirm the context matches the work (UI vs background). |
| 58 | - Ensure `NSManagedObject` instances never cross contexts; pass `NSManagedObjectID` instead. |
| 59 | - If using batch ops, confirm persistent history tracking + merge pipeline. |
| 60 | - If using constraints, confirm merge policy and conflict resolution strategy. |
| 61 | - If performance-related, profile with Instruments and validate fetch batching/limits. |
| 62 | |
| 63 | ## Reference files |
| 64 | |
| 65 | - `references/_index.md` (navigation) |
| 66 | - `references/stack-setup.md` |
| 67 | - `references/saving.md` |
| 68 | - `references/fetch-requests.md` |
| 69 | - `references/threading.md` |
| 70 | - `references/concurrency.md` |
| 71 | - `references/batch-operations.md` |
| 72 | - `references/persistent-history.md` |
| 73 | - `references/model-configuration.md` |
| 74 | - `references/migration.md` |
| 75 | - `references/cloudkit-integration.md` |
| 76 | - `references/performance.md` |
| 77 | - `references/testing.md` |
| 78 | - `references/glossary.md` |