Set up the classes that manage and persist your app’s objects.
Framework
- Core Data
Overview
After you create a data model file as described in Creating a Core Data Model, set up the classes that collaboratively support your app’s model layer. These classes are referred to collectively as the Core Data stack.

An instance of
NSManaged
represents your app’s model file describing your app’s types, properties, and relationships.Object Model An instance of
NSManaged
tracks changes to instances of your app’s types.Object Context An instance of
NSPersistent
saves and fetches instances of your app’s types from stores.Store Coordinator An instance of
NSPersistent
sets up the model, context, and store coordinator all at once.Container
Initialize a Persistent Container
Typically, you initialize Core Data during your app’s startup. Create the persistent container as a lazy variable to defer instantiation until its first use in your app’s delegate.
If you selected the Core Data checkbox when creating a new Xcode project, the template automatically includes this setup code in the App
.
Declare a lazy variable of type
NSPersistent
.Container Create a persistent container instance, passing the data model filename to its initializer.
Load any persistent stores. This call creates a store, if none exists.
class AppDelegate: UIResponder, UIApplicationDelegate {
...
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "DataModel")
container.loadPersistentStores { description, error in
if let error = error {
fatalError("Unable to load persistent stores: \(error)")
}
}
return container
}()
...
}
Once created, the persistent container holds references to the model, context, and store coordinator instances in its managed
, view
, and persistent
properties, respectively.
You can now pass a reference to the container to your user interface.
Pass a Persistent Container Reference to a View Controller
In your app’s root view controller, import Core Data and create a variable to hold a reference to a persistent container.
import UIKit
import CoreData
class ViewController: UIViewController {
var container: NSPersistentContainer!
override func viewDidLoad() {
super.viewDidLoad()
guard container != nil else {
fatalError("This view needs a persistent container.")
}
// The persistent container is available.
}
}
Return to your app’s delegate. In application(_:
, downcast the app window’s root
to your app’s root view controller type. In this reference, set the root view controller’s container
property to the persistent container.
class AppDelegate: UIResponder, UIApplicationDelegate {
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let rootVC = window?.rootViewController as? ViewController {
rootVC.container = persistentContainer
}
return true
}
...
}
To pass the persistent container to additional view controllers, repeat the creation of a container variable in each view controller, and set its value in the previous view controller’s prepare(for:
.
class ViewController: UIViewController {
...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let nextVC = segue.destination as? NextViewController {
nextVC.container = container
}
}
}
Subclass the Persistent Container
NSPersistent
is intended to be subclassed. Your subclass is a convenient place to put Core Data–related code like functions that return subsets of data and calls to persist data to disk.
import CoreData
class PersistentContainer: NSPersistentContainer {
func saveContext(backgroundContext: NSManagedObjectContext? = nil) {
let context = backgroundContext ?? viewContext
guard context.hasChanges else { return }
do {
try context.save()
} catch let error as NSError {
print("Error: \(error), \(error.userInfo)")
}
}
}
The above example adds a save
function to the container, to improve performance by saving the context only when there are changes.
Note
When factoring a model layer into its own framework, an NSPersistent
subclass locates the model file in its own bundle.