Skip to content

dpearson2699/swift-ios-skills / skills/core-data

core-data

Verified

Build, review, or improve Core Data persistence in apps that have not adopted SwiftData. Use when working with NSManagedObject subclasses, NSFetchedResultsController for list-driven UI, NSBatchInsertRequest / NSBatchDeleteRequest / NSBatchUpdateRequest for bulk operations, NSPersistentHistoryChangeRequest for persistent history tracking and multi-target sync, NSStagedMigrationManager for staged schema migrations (iOS 17+), NSCompositeAttributeDescription for composite attributes (iOS 17+), or when integrating Core Data threading with Swift Concurrency. For Core Data + SwiftData coexistence or migration, see the swiftdata skill instead.

FileKindSize
SKILL.mdskill md16.2 KB
evals/evals.jsonconfig3.4 KB
references/persistent-history.mdmarkdown1.9 KB
references/staged-migration.mdmarkdown2.2 KB

references/persistent-history.md

View on GitHub
# Persistent History Across Targets

Use persistent history when an app, extension, widget, or other process writes
the same store and each consumer must merge transactions it has not processed.

## Enable History Tracking

Set the options before loading the persistent store:

```swift
let description = NSPersistentStoreDescription()
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
description.setOption(true as NSNumber,
    forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
container.persistentStoreDescriptions = [description]
```

## Observe, Fetch, Merge, and Purge

```swift
NotificationCenter.default.addObserver(
    self,
    selector: #selector(storeRemoteChange(_:)),
    name: .NSPersistentStoreRemoteChange,
    object: container.persistentStoreCoordinator
)

@objc func storeRemoteChange(_ notification: Notification) {
    let context = container.newBackgroundContext()
    context.perform {
        let request = NSPersistentHistoryChangeRequest.fetchHistory(
            after: self.lastToken
        )
        if let result = try? context.execute(request) as? NSPersistentHistoryResult,
           let transactions = result.result as? [NSPersistentHistoryTransaction] {
            for transaction in transactions {
                self.container.viewContext.mergeChanges(
                    fromContextDidSave: transaction.objectIDNotification()
                )
                self.lastToken = transaction.token
            }
        }
    }
}
```

Persist `lastToken` per target so processing resumes across launches. Purge only
after every relevant consumer has advanced beyond the cutoff; a single target's
latest token is not automatically safe for all targets.

```swift
let purge = NSPersistentHistoryChangeRequest.deleteHistory(before: safeCutoff)
try context.execute(purge)
```

## References

- [NSPersistentHistoryChangeRequest](https://sosumi.ai/documentation/coredata/nspersistenthistorychangerequest)
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall