core-datalisted
Install: claude install-skill thienqo/ios-skills
# Core Data
Build and maintain data persistence using Core Data for apps that have not adopted SwiftData. Covers stack setup, concurrency, batch operations, NSFetchedResultsController, persistent history tracking, staged migration, and testing.
## Contents
- [Stack Setup & Concurrency](#stack-setup--concurrency)
- [NSFetchedResultsController](#nsfetchedresultscontroller)
- [Batch Operations](#batch-operations)
- [Persistent History Tracking](#persistent-history-tracking)
- [Staged Migration & Coexistence](#staged-migration--coexistence)
- [Testing](#testing)
- [Common Mistakes](#common-mistakes)
- [Review Checklist](#review-checklist)
- [References](#references)
## Stack Setup & Concurrency
Core Data contexts are bound to queues: `viewContext` runs on the main queue, while background tasks use `newBackgroundContext()`.
- **Context Isolation**: Always wrap access in `context.perform(_:)` or `context.performAndWait(_:)`.
- **Thread Crossing**: Never pass `NSManagedObject` instances between contexts or threads; pass `NSManagedObjectID` and re-fetch via `existingObject(with:)`.
- **Automatic Merging**: Set `automaticallyMergesChangesFromParent = true` and configure `mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy` on `viewContext`.
```swift
import CoreData
func updateTrip(id: NSManagedObjectID, name: String) async throws {
let context = CoreDataStack.shared.newBackgroundContext()
try await context.perform {
guard let trip = try context.existingObject