Xcode + Core Data: Model file modified by Xcode when Claude Code edits Swift files, causing data loss
Resolved 💬 2 comments Opened Jan 30, 2026 by jaredmorgenstern Closed Feb 28, 2026
Summary
When using Claude Code to edit Swift files in an Xcode project that uses Core Data, with Xcode open, the Core Data model file (.xcdatamodeld) can be modified by Xcode as a side effect of the external file changes. This can lead to Core Data store invalidation and complete data loss.
Environment
- macOS: Darwin 25.2.0
- Claude Code: CLI tool (Opus 4.5)
- Xcode: Open with project loaded during Claude Code session
- Project type: iOS app using Core Data with SQLite persistent store
Steps to Reproduce
- Open an Xcode project that uses Core Data (has
.xcdatamodeldand entity classes) - Keep Xcode open with the project loaded
- Use Claude Code to edit Swift files that reference Core Data entities (e.g., files containing
NSManagedObjectsubclasses,NSFetchRequest,viewContext, etc.) - Claude Code makes edits related to Core Data threading (e.g., adding
newBackgroundContext(),context.perform {}) - Check
git status- the.xcdatamodeld/contentsfile shows as modified despite never being directly edited
Expected Behavior
Editing Swift files should not modify the Core Data model file. The .xcdatamodeld should only change when explicitly edited.
Actual Behavior
- The
.xcdatamodeld/.xcdatamodel/contentsfile was modified (shown asMin git status) - Timestamp showed modification during the Claude Code session (3:22 PM) when only Swift files were being edited
- Neither the user nor Claude Code directly edited the model file
- After
git restoreto fix the model, Core Data created a new empty store, losing all historical data
Evidence
# git status showed:
M TaxRegionTracker.xcdatamodeld/TaxRegionTracker.xcdatamodel/contents
# Error on first build after model was corrupted:
"No NSEntityDescriptions in any model claim the NSManagedObject subclass 'LocationRecord'"
Root Cause Hypothesis
When Xcode is open and detects external file changes (from Claude Code):
- Xcode reloads the modified Swift files
- Xcode sees Core Data references (
LocationRecord,NSFetchRequest, etc.) - Xcode's Core Data tooling touches/modifies the model file (possibly updating codegen settings, timestamps, or entity metadata)
- This modification changes the model's identity/hash
- Core Data sees the store as incompatible with the "new" model and creates a fresh store
Impact
- Severity: Critical - complete data loss
- Data lost: All 2025 historical records (~1,064 location records spanning months)
- Affected workflows: Any iOS developer using Claude Code + Xcode + Core Data
Workaround
Quit Xcode before using Claude Code to edit Swift files in Core Data projects. This prevents Xcode's file monitoring from reacting to external changes.
Recommendation
- Documentation: Warn users about IDE interactions with managed project files (Core Data models, storyboards, XIBs, etc.)
- Detection: Consider detecting when modifying files in projects with open IDEs that have file watchers
- Core Data specific: Consider special handling or warnings when editing Swift files that import CoreData or reference NSManagedObject
Additional Context
- The user confirmed they never opened the model file in Xcode during the session
- Claude Code only edited
.swiftfiles (LocationManager.swift, EmailService.swift, ReportView.swift, etc.) - The edits were related to Core Data threading (switching from
viewContexttonewBackgroundContext()) - Known Xcode bugs exist around
.xccurrentversionhandling and model version reversion, suggesting Xcode's Core Data tooling has edge cases
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗