core-data Core Data Stack

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


This is an implementation of the Core Data Stack which is initially placed in the AppDelegate file if the project is created with Core Data when project is created. These functions can also implemented in separate class for CoreDataStack.swift. One of the major functions is to get the NSManagedObjectContext.


- (NSManagedObjectContext *)managedObjectContext {...}

Swift 2

lazy var managedObjectContext: NSManagedObjectContext = {...}

Swift 3

lazy var persistentContainer: NSPersistentContainer = {...)
let managedObjectContext = persistentContainer.viewContext

The Core Data stack that communicates between the objects in your application and external data stores. The Core Data stack handles all of the interactions with the external data stores so that your application can focus on its business logic. The stack consists of three primary objects: the managed object context (NSManagedObjectContext), the persistent store coordinator (NSPersistentStoreCoordinator), and the managed object model (NSManagedObjectModel).


The NSManagedObjectModel instance describes the data that is going to be accessed by the Core Data stack. NSManagedObjectModel (often referred to as the “mom”) is loaded into memory as the first step in the creation of the stack. An example of the NSManagedObjectModel is DataModel.momd. The NSManagedObjectModel defines the structure of the data


The NSPersistentStoreCoordinator realizes objects from the data in the persistent store and passes those objects off to the requesting NSManagedObjectContext. It creates new instances of the entities in the model, and it retrieves existing instances from a persistent store (NSPersistentStore). The NSPersistentStoreCoordinator also verifies that the data is in a consistent state that matches the definitions in the NSManagedObjectModel.


When you fetch objects from a persistent store, you bring temporary copies onto the scratch pad where they form an object graph (or a collection of object graphs). You can then modify those objects, unless you actually save those changes, however, the persistent store remains unaltered.

All managed objects must be registered with a managed object context. You use the context to add objects to the object graph and remove objects from the object graph. The context tracks the changes you make, both to individual objects’ attributes and to the relationships between objects. By tracking changes, the context is able to provide undo and redo support for you. It also ensures that if you change relationships between objects, the integrity of the object graph is maintained.

When you save changes the context ensures that your objects are in a valid state. The changes are written to the persistent store (or stores), new records are added for objects you created, and records are removed for objects you deleted.

Source: Apple Core Data Programming: Initializing the Core Data Stack

Got any core-data Question?