/reactree-ios-dev:core-data-patternsfrom reactree-ios-dev
Expert decision frameworks for Core Data on iOS/tvOS: choosing persistence layer, context architecture for multi-threading, migration strategy, merge policies, and performance optimization.
Ships in reactree-ios-dev. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Expert decision frameworks for Core Data choices. Claude knows NSPersistentContainer and fetch requests — this skill provides judgment calls for when Core Data fits and architecture trade-offs.
What's your persistence need?
├─ Simple key-value storage
│ └─ UserDefaults or @AppStorage
│ Don't use Core Data for preferences
│
├─ Flat list of Codable objects
│ └─ Is query complexity needed?
│ ├─ NO → File-based (JSON/Plist) or SwiftData
│ └─ YES → Core Data or SQLite
│
├─ Complex relationships + queries
│ └─ How many objects?
│ ├─ < 10,000 → SwiftData (simpler) or Core Data
│ └─ > 10,000 → Core Data (more control)
│
├─ iCloud sync required
│ └─ NSPersistentCloudKitContainer
│ Built-in sync with Core Data
│
└─ Cross-platform (non-Apple)
└─ SQLite directly or Realm
Core Data is Apple-only
The trap: Using Core Data for simple lists. If you don't need relationships, queries, or undo, consider simpler options like SwiftData or file storage.
How many contexts do you need?
├─ Simple app, UI-only operations
│ └─ viewContext only
│ Single context for reads and small writes
│
├─ Background imports/exports
│ └─ viewContext + newBackgroundContext()
│ Background for writes, viewContext for UI
│
├─ Complex with multiple writers
│ └─ Parent-child context hierarchy
│ Rarely needed — adds complexity
│
└─ Sync with server
└─ Dedicated sync context
performBackgroundTask for sync operations
What changed in your model?
├─ Added optional attribute
│ └─ Lightweight migration (automatic)
│
├─ Renamed attribute/entity
│ └─ Lightweight with mapping model hints
│ Set renaming identifier in model
│
├─ Changed attribute type
│ └─ Depends on conversion possibility
│ Int → String: lightweight
│ String → Date: may need custom
│
├─ Added required attribute (no default)
│ └─ Custom migration required
│ Or add default value to make lightweight
│
└─ Complex schema restructuring
└─ Staged migration
Multiple model versions, migrate step by step
What happens on save conflicts?
├─ UI context always wins
│ └─ NSMergeByPropertyObjectTrumpMergePolicy
│ Most common for view context
│
├─ Store (persisted) always wins
│ └─ NSMergeByPropertyStoreTrumpMergePolicy
│ For background sync contexts
│
├─ Need custom resolution
│ └─ Custom merge policy
│ Complex — avoid if possible
│
└─ Fail on conflict
└─ NSErrorMergePolicy (default)
Rarely want this
NEVER use viewContext for heavy operations:
// ❌ Blocks main thread during import
func importUsers(_ data: [UserData]) {
let context = persistenceController.container.viewContext
for item in data {
let user = User(context: context)
user.name = item.name
}
try? context.save() // UI frozen!
}
// ✅ Use background context
func importUsers(_ data: [UserData]) async throws {
try await persistenceController.container.performBackgroundTask { context in
for item in data {
let user = User(context: context)
user.name = item.name
}
try context.save()
}
}
NEVER pass NSManagedObjects between contexts:
// ❌ Object belongs to different context — crash or undefined behavior
let user = fetchUser(in: backgroundContext)
viewContext.delete(user) // Wrong context!
// ✅ Re-fetch in target context using objectID
let user = fetchUser(in: backgroundContext)
let userInViewContext = viewContext.object(with: user.objectID) as! User
viewContext.delete(userInViewContext)
NEVER access managed objects off their context's queue:
// ❌ Thread violation — data corruption possible
let user = fetchUser(in: backgroundContext)
DispatchQueue.main.async {
print(user.name) // Accessing background object on main thread!
}
// ✅ Use context.perform for thread-safe access
backgroundContext.perform {
let user = fetchUser(in: backgroundContext)
let name = user.name
DispatchQueue.main.async {
print(name) // Safe — using local copy
}
}
NEVER ignore save errors:
// ❌ Silent data loss
try? context.save()
// ✅ Handle errors properly
do {
try context.save()
} catch {
context.rollback()
Logger.coreData.error("Save failed: \(error)")
throw error
}
NEVER save after every single change:
// ❌ Performance disaster — disk I/O per object
for item in largeDataset {
let entity = Entity(context: context)
entity.value = item
try context.save() // 10,000 saves!
}
// ✅ Batch changes, save once (or periodically)
for (index, item) in largeDataset.enumerated() {
let entity = Entity(context: context)
entity.value = item
// Save every 1000 objects to manage memory
if index % 1000 == 0 {
try context.save()
context.reset() // Release memory
}
}
try context.save() // Final batch
NEVER call save on context with no changes:
// ❌ Unnecessary disk I/O
func periodicSave() {
try? context.save() // No-op but still has overhead
}
// ✅ Check for changes first
func saveIfNeeded() throws {
guard context.hasChanges else { return }
try context.save()
}
NEVER fetch all objects when you need a count:
// ❌ Loads all objects into memory just to count
let users = try context.fetch(User.fetchRequest())
let count = users.count // May fetch thousands!
// ✅ Use count fetch
let request = User.fetchRequest()
let count = try context.count(for: request)
NEVER fetch everything without limits:
// ❌ May load entire database
let request = User.fetchRequest()
let allUsers = try context.fetch(request)
// ✅ Set appropriate limits
let request = User.fetchRequest()
request.fetchLimit = 50
request.fetchBatchSize = 20 // Loads in batches
NEVER forget to prefetch relationships you'll access:
// ❌ N+1 problem — each post access triggers fault
let request = User.fetchRequest()
let users = try context.fetch(request)
for user in users {
print(user.posts.count) // Separate fetch per user!
}
// ✅ Prefetch relationships
let request = User.fetchRequest()
request.relationshipKeyPathsForPrefetching = ["posts"]
let users = try context.fetch(request)
NEVER assume lightweight migration will work:
// ❌ Crashes on incompatible changes
container.loadPersistentStores { _, error in
if let error = error {
fatalError("Failed: \(error)") // User data lost!
}
}
// ✅ Handle migration failure gracefully
container.loadPersistentStores { description, error in
if let error = error as NSError? {
if error.code == NSMigrationMissingSourceModelError {
// Offer data reset or crash gracefully
Self.resetStore()
}
}
}
@MainActor
final class PersistenceController {
static let shared = PersistenceController()
static let preview = PersistenceController(inMemory: true)
let container: NSPersistentContainer
init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "Model")
if inMemory {
container.persistentStoreDescriptions.first?.url = URL(fileURLWithPath: "/dev/null")
}
// Enable lightweight migration
let description = container.persistentStoreDescriptions.first
description?.shouldMigrateStoreAutomatically = true
description?.shouldInferMappingModelAutomatically = true
container.loadPersistentStores { _, error in
if let error = error {
// In production: log and handle gracefully
fatalError("Core Data load failed: \(error)")
}
}
// View context configuration
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
container.viewContext.undoManager = nil // Disable if not needed
}
func saveViewContext() {
let context = container.viewContext
guard context.hasChanges else { return }
do {
try context.save()
} catch {
Logger.coreData.error("View context save failed: \(error)")
}
}
}
extension PersistenceController {
func importData<T: Decodable>(
_ items: [T],
transform: @escaping (T, NSManagedObjectContext) -> Void
) async throws {
try await container.performBackgroundTask { context in
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
for (index, item) in items.enumerated() {
transform(item, context)
// Batch save to manage memory
if index > 0 && index % 500 == 0 {
try context.save()
context.reset()
}
}
if context.hasChanges {
try context.save()
}
}
}
}
// Usage
try await persistenceController.importData(userDTOs) { dto, context in
let user = User(context: context)
user.id = dto.id
user.name = dto.name
}
struct UserListView: View {
// Basic fetch — automatically updates on changes
@FetchRequest(
sortDescriptors: [SortDescriptor(\.name)],
animation: .default
)
private var users: FetchedResults<User>
var body: some View {
List(users) { user in
Text(user.name ?? "Unknown")
}
}
}
// Dynamic predicate fetch
struct FilteredUserList: View {
@FetchRequest private var users: FetchedResults<User>
init(searchText: String) {
_users = FetchRequest(
sortDescriptors: [SortDescriptor(\.name)],
predicate: searchText.isEmpty ? nil : NSPredicate(
format: "name CONTAINS[cd] %@", searchText
),
animation: .default
)
}
var body: some View {
List(users) { user in
Text(user.name ?? "")
}
}
}
| Need | Solution |
|---|---|
| Simple preferences | UserDefaults |
| Small Codable lists | JSON file or SwiftData |
| Complex queries + relationships | Core Data |
| iCloud sync | NSPersistentCloudKitContainer |
| Cross-platform | SQLite or Realm |
| Context | Use For | Thread |
|---|---|---|
| viewContext | UI reads, small writes | Main |
| newBackgroundContext() | Heavy writes, imports | Background |
| performBackgroundTask | One-off background work | Background |
| Policy | Winner | Use Case |
|---|---|---|
| ObjectTrump | In-memory changes | View context |
| StoreTrump | Persisted data | Sync context |
| ErrorMerge | Neither (fails) | Rarely wanted |
| Change | Automatic? |
|---|---|
| Add optional attribute | ✅ Yes |
| Add attribute with default | ✅ Yes |
| Remove attribute | ✅ Yes |
| Rename (with identifier) | ✅ Yes |
| Change type (compatible) | ✅ Maybe |
| Add required (no default) | ❌ No |
| Change relationship type | ❌ No |
| Smell | Problem | Fix |
|---|---|---|
| viewContext for imports | Main thread blocked | Use background context |
| NSManagedObject across contexts | Wrong thread access | Re-fetch via objectID |
| try? context.save() | Silent data loss | Handle errors |
| Save per object in loop | Disk I/O explosion | Batch saves |
| fetch() for count | Memory waste | context.count(for:) |
| No fetchLimit | Loads entire DB | Set reasonable limits |
| Missing prefetch | N+1 fetches | relationshipKeyPathsForPrefetching |
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
27 skills in this plugin
/accessibility-patternsExpert accessibility decisions for iOS/tvOS: when to combine vs separate elements, label vs hint selection, Dynamic Type layout strategies, and WCAG AA compliance trade-offs. Use when implementing VoiceOver support, handling Dynamic Type, or ensuring accessibility compliance. Trigger keywords: accessibility, VoiceOver, Dynamic Type, WCAG, a11y, accessibilityLabel, accessibilityElement, accessibilityTraits, isAccessibilityElement, reduceMotion, contrast, focus/alamofire-patternsExpert Alamofire decisions for iOS/tvOS: when Alamofire adds value vs URLSession suffices, interceptor chain design trade-offs, retry strategy selection, and certificate pinning considerations. Use when designing network layer, implementing auth token refresh, or choosing between networking approaches. Trigger keywords: Alamofire, URLSession, interceptor, RequestAdapter, RequestRetrier, certificate pinning, Session, network layer, token refresh, retry/api-integrationExpert API integration decisions for iOS/tvOS: REST vs GraphQL trade-offs, API versioning strategies, caching layer design, and offline-first architecture choices. Use when designing network architecture, implementing offline support, or choosing between API patterns. Trigger keywords: REST, GraphQL, API versioning, caching, offline-first, URLSession, background fetch, ETag, pagination, rate limiting/app-lifecycleExpert lifecycle decisions for iOS/tvOS: when SwiftUI lifecycle vs SceneDelegate, background task strategies, state restoration trade-offs, and launch optimization. Use when managing app state transitions, handling background work, or debugging lifecycle issues. Trigger keywords: lifecycle, scenePhase, SceneDelegate, AppDelegate, background task, state restoration, launch time, didFinishLaunching, applicationWillTerminate, sceneDidBecomeActive/atomic-design-iosExpert Atomic Design decisions for iOS/tvOS: when component hierarchy adds value vs overkill, atom vs molecule boundary judgment, design token management trade-offs, and component reusability patterns. Use when structuring design systems, deciding component granularity, or organizing component libraries. Trigger keywords: Atomic Design, atoms, molecules, organisms, templates, component library, design system, design tokens, reusability, composition/clean-architecture-iosExpert Clean Architecture decisions for iOS/tvOS: when Clean Architecture adds value vs overkill, layer boundary judgment calls, dependency rule violations to catch, and practical trade-offs between purity and pragmatism. Use when designing app architecture, debugging layer violations, or deciding what belongs where. Trigger keywords: Clean Architecture, layer, domain, data, presentation, use case, repository, dependency rule, entity, DTO, mappernpx claudepluginhub kaakati/rails-enterprise-dev --plugin reactree-ios-devOther plugins' skills for the same job