# Debug Core Data Issues on a Rented Remote Mac

Canonical: https://figment.so/blog/mac-core-data-debugger-remote

Enable -com.apple.CoreData.SQLDebug 1 and ConcurrencyDebug 1 through an Xcode test plan, run xcodebuild test over SSH, and rank slow fetches with awk. Figment's Intel Mac VM covers Xcode 26.3 or earlier.

**Enable `-com.apple.CoreData.SQLDebug 1` and `-com.apple.CoreData.ConcurrencyDebug 1` through an Xcode test plan, run `xcodebuild test` on the remote Mac, and sort the output with `awk`.** Core Data prints a verbose trace from runtime launch arguments without a single code change. Drive them from the terminal: opening the Xcode UI over a remote desktop is slower than SSH. Reference material: Apple's [Core Data documentation](https://developer.apple.com/documentation/coredata) and the Xcode guide [Diagnosing and resolving bugs in your running app](https://developer.apple.com/documentation/xcode/diagnosing-and-resolving-bugs-in-your-running-app).

## Which Core Data debug flags should I turn on?

There are three flags, and each one logs something different:

| Argument | Value | What it logs |
|----------|-------|-------------|
| `-com.apple.CoreData.SQLDebug` | `1`–`3` | SQL issued to the SQLite store. Level 1 prints statement text; higher levels add binding parameters and row counts. |
| `-com.apple.CoreData.MigrationDebug` | `1` | Lightweight migration steps: entity mapping, attribute transforms, model hashes. |
| `-com.apple.CoreData.ConcurrencyDebug` | `1` | Faults when a managed object is touched from the wrong queue; crashes with a stack trace on misuse. |

Start with `SQLDebug 1` and `ConcurrencyDebug 1`. Add `MigrationDebug 1` only while you investigate a model change, since otherwise it buries the SQL trace.

## How do I inject the arguments through a test plan?

`xcodebuild test` reads launch arguments from the scheme or from a `.xctestplan` file. A checked-in test plan gives repeatable runs on a remote Mac. Create `CoreDataDebug.xctestplan` at the project root:

```json
{
  "configurations": [
    {
      "id": "coredata-sql-debug",
      "name": "Core Data SQL Debug",
      "options": {
        "commandLineArguments": [
          { "argument": "-com.apple.CoreData.SQLDebug 1",         "enabled": true },
          { "argument": "-com.apple.CoreData.ConcurrencyDebug 1", "enabled": true }
        ]
      }
    }
  ],
  "defaultOptions": {},
  "testTargets": [],
  "version": 1
}
```

Leave `testTargets` empty to inherit the scheme's targets, or list specific target references to scope the run. Confirm the field format against a `.xctestplan` that Xcode generates from the IDE, because the schema is not formally documented and has changed between versions.

List simulators on the remote Mac, then run the tests with your project's real scheme name:

```bash
xcrun simctl list devices available | grep -i iphone

xcodebuild test \
  -project YourApp.xcodeproj \
  -scheme "$YOUR_SCHEME" \
  -destination 'platform=iOS Simulator,name=iPhone 16' \
  -testPlan CoreDataDebug \
  2>&1 | tee coredata-test.log
```

Swap `-project` for `-workspace YourApp.xcworkspace` if you use CocoaPods or SwiftPM through a workspace. `tee` keeps the log, so analysis needs no second test run.

## How do I rank the slowest fetches with awk?

Core Data prints two lines per executed statement: an `sql:` line and an `annotation:` line with the time. Pair them and sort:

```bash
awk '
  /CoreData: sql:/        { stmt = substr($0, index($0, "sql: ") + 5) }
  /annotation: sql execution time/ {
    t = $6; sub(/s,?$/, "", t)
    printf "%8.4fs  %s\n", t+0, stmt
  }
' coredata-test.log | sort -rn | head -20
```

What to act on:

- **Over 10 ms per fetch** on a simulator usually means a table scan. Mark the attribute as Indexed in the model editor and re-run.
- **The same `SELECT ... WHERE Z_PK = ?` repeated in a loop** is the classic N+1. Set `relationshipKeyPathsForPrefetching` on the fetch request.

Group identical query templates to count repeats:

```bash
grep 'CoreData: sql: SELECT' coredata-test.log \
  | sed 's/ WHERE Z_PK = ?.*/ WHERE .../' \
  | sort | uniq -c | sort -rn | head -10
```

Timings from a VM reflect shared virtual CPUs. Use them to compare queries within one run, and treat absolute numbers as indicative.

## How do I see fault firing and save timelines?

Open Instruments on the remote Mac's desktop over the remote desktop session and pick the **Core Data** template. It shows each fetch request as a bar scaled to its duration, each fault fire as a discrete event, and `save()` durations per context. Line it up with Time Profiler to attribute UI stutter to faults firing on the main thread. For performance numbers you intend to compare, use a Dedicated M4 Pro, because Instruments in a VM reports virtualized hardware.

## How do I provoke a merge conflict on purpose?

Save the same object from two contexts without merging. With `ConcurrencyDebug 1` on, a cross-queue managed-object access crashes with a stack trace, which is the behavior you want in CI.

```swift
try backgroundContext.save()   // succeeds; stamps the row version
try mainContext.save()         // throws NSManagedObjectMergeError
```

`mainContext.save()` returns an `NSError` in `NSCocoaErrorDomain`. Look the numeric code up in the Core Data error header of your installed SDK instead of hard-coding one, because a few codes have shifted across SDK versions. Set `mainContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy` and re-run to confirm the policy resolves it.

## How do I validate a migration before shipping?

Run it against a representative seed store on the remote simulator before you raise a model version. Boot the simulator, install the app, copy the seed `.sqlite` into the app container, then run the test plan with `MigrationDebug 1` added. Grep the log for `CoreData: migration:` lines and any `error:` lines. A lightweight success prints a "Successfully migrated" entry, and a mismatch between source and destination model hashes needs a mapping model.

## When is the Intel Mac VM enough for Core Data debugging?

The [Intel Mac VM](/mac-hosting#intel-vm) ($25 a month, $2 a day) runs macOS Sequoia, where Apple lists Xcode 26.0 to 26.3, and it is enough when your scenario reproduces on x86_64 simulators. Choose the [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) ($39 a month, $6 a day) when you need Xcode 26.4 or later or the arm64 simulator slice, and the [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro) ($349 a month) for timing you want to trust. Check the macOS version shown before purchase against [Apple's Xcode requirements](https://developer.apple.com/xcode/system-requirements).

Twelve-month list prices, for scale: Intel Mac VM 12 × $25 = $300; Apple Silicon Mac VM 12 × $39 = $468; Dedicated M4 Pro 48 GB $3,228 paid up front for 12 months. Competitors, as checked on 2026-11-10: [CloudMac M4/16GB/256GB](https://cloudmac.org/pricing) is $99.99 a month, or [$999 a year](https://cloudmac.org/) prepaid ($83.25 a month; the 256GB row showed Reserve now and the 512GB and 1TB rows showed fully reserved), and [MacStadium M4.S](https://macstadium.com/pricing) is $149 a month (16GB/256GB, support and network included per its pricing page). The CloudMac and MacStadium rows are whole Macs, so the Dedicated M4 Pro is the like-for-like Figment plan. Plans are at [/mac-hosting](/mac-hosting).

---

*Related: [XCTest Performance Testing on a Remote Mac](/blog/mac-xctest-performance-remote) · [UI Testing on a Remote Mac with Xcode](/blog/mac-ui-testing-xcode-remote-mac) · [GitHub Actions Self-Hosted Runner on Mac](/blog/mac-github-actions-self-hosted-runner)*
