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

  1. Open an Xcode project that uses Core Data (has .xcdatamodeld and entity classes)
  2. Keep Xcode open with the project loaded
  3. Use Claude Code to edit Swift files that reference Core Data entities (e.g., files containing NSManagedObject subclasses, NSFetchRequest, viewContext, etc.)
  4. Claude Code makes edits related to Core Data threading (e.g., adding newBackgroundContext(), context.perform {})
  5. Check git status - the .xcdatamodeld/contents file 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

  1. The .xcdatamodeld/.xcdatamodel/contents file was modified (shown as M in git status)
  2. Timestamp showed modification during the Claude Code session (3:22 PM) when only Swift files were being edited
  3. Neither the user nor Claude Code directly edited the model file
  4. After git restore to 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):

  1. Xcode reloads the modified Swift files
  2. Xcode sees Core Data references (LocationRecord, NSFetchRequest, etc.)
  3. Xcode's Core Data tooling touches/modifies the model file (possibly updating codegen settings, timestamps, or entity metadata)
  4. This modification changes the model's identity/hash
  5. 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

  1. Documentation: Warn users about IDE interactions with managed project files (Core Data models, storyboards, XIBs, etc.)
  2. Detection: Consider detecting when modifying files in projects with open IDEs that have file watchers
  3. 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 .swift files (LocationManager.swift, EmailService.swift, ReportView.swift, etc.)
  • The edits were related to Core Data threading (switching from viewContext to newBackgroundContext())
  • Known Xcode bugs exist around .xccurrentversion handling and model version reversion, suggesting Xcode's Core Data tooling has edge cases

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗