core-data-expert
core-data-expert equips AI coding assistants with battle-tested Core Data patterns drawn from production WeTransfer apps and WWDC best practices. Navigate threading pitfalls, optimize query performance, and handle schema migrations with confidence—all through concise, agent-ready references designed for immediate triage and safe defaults.
core-data-expert recommends using NSManagedObjectContext with the correct concurrency type (privateQueue or mainQueue) and never accessing managed objects across threads without proper context confinement. For Swift Concurrency, wrap Core Data operations in context.perform or use @MainActor for main-thread contexts. Always fetch objects on the same context where you'll use them, and use objectID for cross-context references instead of passing NSManagedObject instances directly.
AI-generated summary based on this skill's SKILL.md
Install
AvdLee/Core-Data-Agent-Skill/core-data-expert
git clone https://github.com/AvdLee/Core-Data-Agent-Skill
cp -r Core-Data-Agent-Skill/core-data-expert ~/.claude/skills/core-data-expertFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How do I fix Core Data threading crash issues?
core-data-expert recommends using NSManagedObjectContext with the correct concurrency type (privateQueue or mainQueue) and never accessing managed objects across threads without proper context confinement. For Swift Concurrency, wrap Core Data operations in context.perform or use @MainActor for main-thread contexts. Always fetch objects on the same context where you'll use them, and use objectID for cross-context references instead of passing NSManagedObject instances directly.
What is the correct Core Data stack setup for iOS?
core-data-expert's recommended setup uses NSPersistentContainer, which handles the coordinator and store automatically. Create it once at app launch, typically in your app delegate or a singleton. For CloudKit sync, use NSPersistentCloudKitContainer instead. Always initialize on the main thread, then perform background operations via newBackgroundContext(). This pattern ensures thread safety and proper resource management from the start.
How do I use Core Data with Swift Concurrency?
core-data-expert advises wrapping all Core Data work in context.perform(schedule:_:) or context.performAndWait(_:) blocks, marking main-thread contexts with @MainActor. For async/await, use context.perform { @MainActor in ... } or create a background context and call its perform method. Ensure NSManagedObject subclasses conform to Sendable where needed, and never pass managed objects between isolation domains—use objectID instead.
What causes Core Data batch operations to not update the UI?
core-data-expert identifies the root cause: batch operations (NSBatchUpdateRequest, NSBatchDeleteRequest) bypass in-memory contexts, so NSFetchedResultsController and observers don't see changes. After a batch operation, either refresh the context with refreshAllObjects() or use persistent history tracking to merge changes back. For production apps, enable NSPersistentHistoryTrackingRequest and process history tokens to keep UI in sync.
How do I set up Core Data migration and resolve versioning errors?
core-data-expert recommends starting with lightweight migration (automatic schema mapping) by creating a new model version in Xcode and letting Core Data infer changes. For complex migrations, use staged migration or custom mapping models. Always increment the model version number and set the current version in the data model inspector. Test migrations on real device data; use persistent store coordinator options to enable automatic migration during container initialization.
How do I integrate CloudKit sync with NSPersistentCloudKitContainer?
core-data-expert advises replacing NSPersistentContainer with NSPersistentCloudKitContainer and enabling CloudKit capability in Xcode. Set a container identifier matching your iCloud container. Core Data handles sync automatically, but monitor CKQueryOperationMaximumResults and use persistent history tracking for conflict resolution. Test with CloudKit dashboard, handle network errors gracefully, and remember that CloudKit sync requires a valid iCloud account on device.
SKILL.md
rendered from the published skill — quoted content, verbatim
Core Data Expert
Fast, production-oriented guidance for building correct, performant Core Data stacks and fixing common crashes.
Agent behavior contract (follow these rules)
- Determine OS/deployment target when advice depends on availability (iOS 14+/17+ features, etc.).
- Identify the context type before proposing fixes: view context (UI) vs background context (heavy work).
- Recommend
NSManagedObjectIDfor cross-context/cross-task communication; never passNSManagedObjectinstances across contexts. - Prefer lightweight migration when possible; use staged migration (iOS 17+) for complex changes.
- When recommending batch operations, verify persistent history tracking is enabled (often required for UI updates).
- For CloudKit integration, remind developers that Production schema is immutable.
- Reference WWDC/external resources
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 15 files
core-data-expert/SKILL.md
core-data-expert/references/_index.md
core-data-expert/references/batch-operations.md
core-data-expert/references/cloudkit-integration.md
core-data-expert/references/concurrency.md
core-data-expert/references/fetch-requests.md
core-data-expert/references/glossary.md
core-data-expert/references/migration.md
core-data-expert/references/model-configuration.md
core-data-expert/references/performance.md
core-data-expert/references/persistent-history.md
core-data-expert/references/project-audit.md
core-data-expert/references/saving.md
core-data-expert/references/stack-setup.md
core-data-expert/references/testing.md