Mastering In CoreData (Part 2 Core Data Stack)

Core data

Core Data Stack

It’s very important to understand Core Data stack. Many developers don’t take time to learn about it which makes their life hell while using Core Data. The Core Data stack is a collection of framework objects that are accessed as part of the initialization of Core Data and that mediate between the objects in your application and external data stores. This stack provides all the features or task associated with data model layer objects. The features we discussed in part 1

A Core Data stack is composed of the following objects → one or more managed object contexts connected to a single persistent store coordinator which is in turn connected to one or more persistent stores.

Core data Stack

Managed Object Context (MOC)

Its primary responsibility is to manage a collection of Managed Objects (Entities). A Managed Object Context maintains the state of entities, typically in memory.

When an operation on such objects is completed, you save such MOC, typically down to the Persistent Store coordinator to persistent store

It’s sit on top of Core Data stack and your application will interact with the most, and therefore it is the one that is exposed to the rest of your application

Application can create one or more MOC per stack. Context is connected to persistent store coordinator or from parent context

It provides Caching, Change tracking, Lazy loading, Redo, Undo and validation features

Persistent Store Coordinator (PSC)

Instance of NSPersistentStoreCoordinator. The coordinator is the central object in a Core Data stack.

The persistent store coordinator role is to manage multiple/single stores and present to its managed object contexts the facade of a single unified store as shown below in the figure coordinator communicating to multiple stores.

It has a reference to a managed object model as shown below in the figure 2.1. Persistent store coordinator can talk or have a reference of the majority of the components in a stack

Figure 2.1

Persistent store

You can think of a persistent store as a database file (Sqlite) where individual records each hold the last-saved values of a managed object (Entity).

Core Data also provides an in-memory store that lasts no longer than the lifetime of a process

We can make multiple persistent stores per stack

Core Data offers three native file types for a persistent store: binary, XML, and SQLite. You can implement your own store type if you want Core Data to interoperate with a custom file format or server

Managed Object

Instance of NSManagedObject. It represents a record from a persistent store

A managed object is registered with a managed object context. In any given context, there is at most one instance of a managed object that corresponds to a given record in a persistent store

Managed object model

A managed object model is a set of objects that together form a blueprint describing the managed objects you use in your application

Schema of the model objects you use in your application

Persistent object store

A persistent object store maps between objects in your application and records in a persistent store.

There are different classes of persistent object store for the different file types that Core Data supports

You can also implement your own if you want to support a custom file type

You don’t create a persistent object store directly. Instead, Core Data creates a store of the appropriate type for you when you send
an addPersistentStoreWithType:configuration:URL:options:error: message to a persistent store coordinator.


In this part we discussed Core data stack. This part purpose is to put theoretical things in one place. If you don’t understand anything about core data stack don’t worry when we will be doing coding part these points will get clear when I will be referencing these points.
But you should take this point that Core data stack is essential for the working of Core data . All features that Core Data provides with the help of this stack. Core Data stack have many components each have their role . The top component is the MOC and the bottom component is the persistent store in which actual data is store.

Useful Links