Core Data and SwiftData are Apple's on-device persistence frameworks for iOS, iPadOS, macOS, watchOS, and tvOS apps, sitting on top of SQLite (or an in-memory store) while managing an object graph in memory. They exist because almost every real app needs to survive a relaunch: cached API results, user-created content, offline-first state, and anything that should still be there tomorrow all need somewhere durable to live that's faster and richer than flat files. The key mental model to keep in mind is that neither framework saves anything until you explicitly commit a context — objects live safely in an in-memory scratchpad (fast to mutate, cheap to discard) and are only written to disk on save(), and that same context is also the boundary that defines thread-safety, undo scope, and how CloudKit conflicts get resolved.
What This Cheat Sheet Covers
This topic spans 12 focused tables and 118 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Core Data Stack Fundamentals
Core Data's stack is the plumbing that turns a data model file into a working, queryable object graph; NSPersistentContainer collapses what used to be 15-20 lines of setup into a few, and almost everything else in Core Data builds on the pieces it hands you.
| Component | Example | Description |
|---|---|---|
let container = NSPersistentContainer(name: "Model")container.loadPersistentStores { _, error in } | Bundles the model, store coordinator, and a main-queue context into one object; call loadPersistentStores once at launch. | |
let context = container.viewContext | In-memory scratchpad for creating, editing, and fetching objects on the main queue before they're saved to disk. | |
if context.hasChanges { try? context.save()} | Writes all pending inserts, updates, and deletes to the persistent store in one transaction; check hasChanges first to avoid wasted work. | |
container.managedObjectModel | Describes every entity, attribute, and relationship; loaded from the .xcdatamodeld file bundled with the app. | |
container.persistentStoreCoordinator | Mediates between the managed object model and the actual store file(s) on disk, translating requests into store-level operations. |