$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
Swift / iOSintermediateNew

Core Data

Share

Set up Core Data with models, contexts, and fetch requests

Works with OpenClaude

You 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 .xcdatamodeld file 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

  1. Create your Core Data model file by right-clicking in Xcode Project Navigator → New File → Data Model, name it YourAppName.xcdatamodeld
  2. In the model editor, add an Entity (click the "+" button at bottom left) and name it (e.g., Task)
  3. Add Attributes to the entity by clicking the "+" under Attributes and set their types (e.g., title as String, isComplete as Boolean, createdDate as Date)
  4. Set the Codegen to "Class Definition" in the Data Model Inspector (right panel) for automatic NSManagedObject subclass generation
  5. Create a Core Data manager class that initializes NSPersistentContainer and provides contexts for reading and writing
  6. Use viewContext (the main thread context) for UI updates and newBackgroundContext() for background operations
  7. Build fetch requests with NSFetchRequest<T> specifying your entity name and add NSPredicate filters if needed
  8. 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

Quick Info

CategorySwift / iOS
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
swiftcore-datapersistence

Install command:

curl -o ~/.claude/skills/swift-core-data.md https://clskills.in/skills/swift/swift-core-data.md

Related Swift / iOS Skills

Other Claude Code skills in the same category — free to download.

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.