Core Data Safety Audit
Scan your Swift codebase and Core Data models for the 5 most common Core Data issues that cause production crashes and data loss.
What This Command Checks
- Schema Migration Safety — Missing lightweight migration options, schema version mismatches
- Thread-Confinement Violations — NSManagedObject accessed from wrong threads
- N+1 Query Patterns — Relationship access in loops without prefetching
- Production Risk Patterns — Delete store patterns, missing migration testing
- Performance Issues — Missing batch sizes, no faulting controls
When to Use
Run this command when:
- Adding new Core Data entities or attributes
- Before shipping production releases
- After seeing intermittent crashes in production
- When users report data loss or corruption
- Before migrating to a new Core Data schema version
Issue Categories
🔴 Critical Issues (Crashes/Data Loss)
Schema Migration Safety
- Missing
NSMigratePersistentStoresAutomaticallyOption - Store deletion without migration path (100% data loss)
- Schema changes without version increments
Thread-Confinement Violations
- NSManagedObject accessed outside
perform/performAndWait - Objects passed between threads or actors
- Background fetch without proper context usage
🟡 Medium Priority (Performance)
N+1 Query Patterns
- Relationship access inside loops
- Missing
relationshipKeyPathsForPrefetching - Fetch requests that trigger hundreds of queries
Production Risk Patterns
- Hard-coded store paths
- Missing migration unit tests
- No rollback strategy for failed migrations
🟢 Low Priority (Optimization)
Performance Issues
- Missing fetch batch sizes
- No faulting controls on large datasets
- Inefficient predicate patterns
Running the Audit
bash
# In Claude Code
/audit-core-dataThe command will:
- Find all Swift files and Core Data models
- Scan for the 5 issue categories above
- Report findings with
file:linereferences - Prioritize by severity (Critical → Low)
- Link to relevant sections in the core-data-diag skill
Example Output
🔴 CRITICAL: Schema Migration Safety (3 issues)
- AppDelegate.swift:45 - Missing migration options
- CoreDataStack.swift:23 - Store deletion detected
- User.xcdatamodeld — Schema version not incremented
🟡 MEDIUM: N+1 Query Patterns (7 issues)
- UserListView.swift:67 - Relationship access in loop
- PostsViewController.swift:102 - Missing prefetch
🟢 LOW: Performance Issues (2 issues)
- FetchController.swift:34 - Missing batch sizeNext Steps
After running the audit:
- Fix Critical issues immediately — These cause production crashes
- Review Medium issues — Address before next release
- Document Low issues — Add to technical debt backlog
For detailed fix guidance, use the core-data-diag skill:
"How do I fix these Core Data migration issues?"The skill provides:
- Safe migration patterns
- Thread-confinement solutions
- N+1 query prevention
- Production crisis defense strategies
Real-World Impact
Before audit
- 2-5 hours debugging intermittent crashes
- Data loss discovered in production
- Migration failures affecting 500K+ users
After audit
- 2-5 minutes to identify issues
- Catch problems before production
- Proactive migration testing prevents data loss
Run this command before every release to catch Core Data regressions early.