Set up Core Data with models, contexts, and fetch requests
✓Works with OpenClaudeYou are a Swift iOS developer. The user wants to set up Core Data with models, contexts, and fetch requests to persist data in their app.
What to check first
- Open your
.xcdatamodeldfile in Xcode (or create one via File → New → Data Model) - Verify the iOS deployment target is 11.0 or higher in Build Settings
- Check that CoreData framework is already linked (it is by default in iOS projects)
Steps
- Create your Core Data model file by right-clicking in Xcode Project Navigator → New File → Data Model, name it
YourAppName.xcdatamodeld - In the model editor, add an Entity (click the "+" button at bottom left) and name it (e.g.,
Task) - Add Attributes to the entity by clicking the "+" under Attributes and set their types (e.g.,
titleas String,isCompleteas Boolean,createdDateas Date) - Set the Codegen to "Class Definition" in the Data Model Inspector (right panel) for automatic NSManagedObject subclass generation
- Create a Core Data manager class that initializes
NSPersistentContainerand provides contexts for reading and writing - Use
viewContext(the main thread context) for UI updates andnewBackgroundContext()for background operations - Build fetch requests with
NSFetchRequest<T>specifying your entity name and addNSPredicatefilters if needed - Execute fetches on the appropriate context using
fetch()and wrap in do-catch for error handling
Code
import CoreData
import UIKit
class CoreDataManager {
static let shared = CoreDataManager()
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "YourAppName")
container.loadPersistentStores { _, error in
if let error = error as NSError? {
fatalError("Unresolved error: \(error), \(error.userInfo)")
}
}
return container
}()
var viewContext: NSManagedObjectContext {
return persistentContainer.viewContext
}
func saveContext() {
let context = viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let error = error as NSError
print("Save error: \(error), \(error.userInfo)")
}
}
}
func fetchTasks(predicate: NSPredicate? = nil) -> [Task] {
let request: NSFetchRequest<Task> = Task.fetchRequest()
request.predicate = predicate
do {
return try viewContext.fetch(request)
} catch {
print("Fetch error: \(error)")
return []
}
}
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Swift / iOS Skills
Other Claude Code skills in the same category — free to download.
SwiftUI
Build SwiftUI views with state management and navigation
UIKit
Create UIKit view controllers with Auto Layout
Swift Networking
Build networking layer with URLSession and Codable
Swift Combine
Use Combine for reactive programming in Swift
Swift Testing
Write XCTest unit and UI tests for iOS apps
Swift Async/Await Concurrency
Use Swift's structured concurrency for async code without callback hell
Swift CoreData Relationships
Model one-to-many and many-to-many relationships in CoreData
Want a Swift / iOS skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.