Skills DirectorySkills Directory
SkillsLearnSecurityCategoriesDocsCommunityBlog
Sign InSubmit Skill
Skills Directory

Security-tested agent skills for Claude, coding agents, and AI workflows.

Directory

  • Browse Skills
  • All Skills A–Z
  • Claude Skills
  • Claude Code Skills
  • Agent Skills
  • Categories
  • Authors
  • Submit a Skill

Learn

  • Learn Hub
  • Install Claude Skills
  • Write SKILL.md
  • Skills vs MCP
  • Directories Compared

Security

  • Security
  • Methodology
  • Secure Claude Skills
  • Security Badges

Company

  • About
  • Community
  • Blog
  • API Docs
  • Advertise

2026 Skills Directory. All rights reserved.

ProTermsPrivacyRefunds
Back to skills

Core Data

ASecurity

Build or review persistence in apps that still use Core Data, including managed objects, fetched results, batch operations, persistent history, staged migration, and concurrency. Use for Core Data-only work; route SwiftData adoption or coexistence to swiftdata.

3 stars
0 votes
0 copies
0 views
Added 9/28/2026
developmentgoswiftsqltestingdatabasedocumentation

Security Analysis

A100/100

Scanned 9/28/2026

Install to Claude Code

$npx -y skills add thiennc-tesoglobal/ios-skills --skill core-data --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Core Data?

Add the live security badge to your README — it updates automatically with every re-scan.

Security grade badge for Core Data
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/thiennc-tesoglobal-core-data/badge)](https://www.skillsdirectory.com/skills/thiennc-tesoglobal-core-data)

More formats (shields.io, HTML) on the badges page.

Files
SKILL.md
---
name: core-data
description: "Build or review persistence in apps that still use Core Data, including managed objects, fetched results, batch operations, persistent history, staged migration, and concurrency. Use for Core Data-only work; route SwiftData adoption or coexistence to swiftdata."
---

# 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(with: id) as? CDTrip else { return }
        trip.name = name
        if context.hasChanges { try context.save() }
    }
}
```

## NSFetchedResultsController

Drives UI collections from an `NSFetchRequest` with automatic change tracking:

- Always supply at least one sort descriptor in the fetch request.
- Use diffable snapshots (`controller(_:didChangeContentWith:)`) on iOS 13+ to animate UI changes.
- Delete the cache with `deleteCache(withName:)` before changing predicate or sort descriptors, or pass `cacheName: nil`.

## Batch Operations

`NSBatchInsertRequest`, `NSBatchUpdateRequest`, and `NSBatchDeleteRequest` execute directly at the SQLite level, bypassing managed object contexts.

- Set `resultType = .objectIDs` / `.resultTypeObjectIDs`.
- Merge results manually into live contexts using `NSManagedObjectContext.mergeChanges(fromRemoteContextSave:into:)`.
- Note that batch deletes bypass Core Data relationship delete rules (e.g. `Deny`).

## Persistent History Tracking

Track store modifications across app extensions, widgets, and processes:

- Enable `NSPersistentHistoryTrackingKey: true` in store options.
- Query changes using `NSPersistentHistoryChangeRequest` and merge them into local contexts.
- Purge consumed transactions periodically with history truncation requests to prevent database bloat.
- Read [references/persistent-history.md](references/persistent-history.md) for full transaction tracking and purge loops.

## Staged Migration & Coexistence

- **Staged Migration (iOS 17+)**: Use `NSStagedMigrationManager` with lightweight and custom stages (`NSCustomMigrationStage`) for deterministic upgrades. Read [references/staged-migration.md](references/staged-migration.md).
- **SwiftData Boundary**: When sharing a database file with SwiftData, point both to the same store URL, preserve model schemas and attribute names, and map renames with `@Attribute(originalName:)`. Route pure SwiftData work to the `swiftdata` skill.

## Testing

Use `NSInMemoryStoreType` for deterministic unit testing. Share a single compiled `NSManagedObjectModel` instance across tests to prevent duplicate entity runtime errors.

## Common Mistakes

- **Passing NSManagedObject across threads**: Causes concurrency crashes. Always pass `NSManagedObjectID` and refetch with `existingObject(with:)`.
- **Missing mergeChanges after batch requests**: Batch operations bypass memory contexts. Always merge returned object IDs.
- **Calling save() without hasChanges**: Avoid redundant I/O; guard saves with `if context.hasChanges`.
- **Omitting merge policy on viewContext**: Missing `mergePolicy` causes conflict save crashes. Set `NSMergeByPropertyObjectTrumpMergePolicy`.
- **Marking NSManagedObject as Sendable**: Managed objects are queue-bound. Do not mark `@unchecked Sendable`.

## Review Checklist

- [ ] `NSPersistentContainer` created once and shared across the app
- [ ] Context access strictly guarded by `perform(_:)` or `performAndWait(_:)`
- [ ] No `NSManagedObject` instances cross thread or context boundaries
- [ ] `viewContext.automaticallyMergesChangesFromParent` is enabled
- [ ] `mergePolicy` configured on contexts to handle conflicts cleanly
- [ ] Batch operation results merged into active contexts via `mergeChanges`
- [ ] `NSFetchedResultsController` requests include explicit sort descriptors
- [ ] In-memory test stores reuse a shared `NSManagedObjectModel`

## References

- Implementation patterns (Stack, FRC, Batch, Testing): [references/core-data-patterns.md](references/core-data-patterns.md)
- Cross-process tracking: [references/persistent-history.md](references/persistent-history.md)
- Staged migration: [references/staged-migration.md](references/staged-migration.md)
- [Core Data](https://sosumi.ai/documentation/coredata)
- [NSPersistentContainer](https://sosumi.ai/documentation/coredata/nspersistentcontainer)
- [NSFetchedResultsController](https://sosumi.ai/documentation/coredata/nsfetchedresultscontroller)
- [NSStagedMigrationManager](https://sosumi.ai/documentation/coredata/nsstagedmigrationmanager)

Attribution

thiennc-tesoglobalthiennc-tesoglobal
View sourceMore from thiennc-tesoglobal →
SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments (0)

No comments yet. Be the first to comment!

SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Related Skills

Browser Extension Developer

Use this skill when developing or maintaining browser extension code in the `browser/` directory, including Chrome/Firefox/Edge compatibility, content scripts, background scripts, or i18n updates.

284972 votes

Seo Optimizer

SEO optimization with keyword analysis, readability assessment, technical validation, content quality. Use for search rankings, blog posts, content audits, or encountering keyword density, readability scores, meta tags, schema markup errors.

2222 votes

Google Official Seo Guide

Official Google SEO guide covering search optimization, best practices, Search Console, crawling, indexing, and improving website search visibility based on official Google documentation

1862 votes

Tanstack Start

Build a full-stack TanStack Start app on Cloudflare Workers from scratch — SSR, file-based routing, server functions, D1+Drizzle, better-auth, Tailwind v4+shadcn/ui. Use whenever the user mentions TanStack Start, asks to scaffold a full-stack Cloudflare app with SSR, wants an SSR dashboard, or asks for a React 19 + Cloudflare Workers app with file-based routing and server functions — even if they don't name TanStack Start specifically. No template repo — Claude generates every file fresh per ...

10311 votes

Pentest

PTES-aligned adversarial security audit for backend, frontend, and mobile applications. Produces a CVSS-scored Hacker Report with verified PoCs and phased remediation.

5491 votes
View all in development →