Core Data is a framework in Apple’s various OS SDK including, but not limited to iOS and OS X. It has two major roles a model layer and a persistence layer. The model layer is used in the management of model objects and persist data. Simply you can store and manage data in an object-oriented interface. Primary features include filtering, querying, sorting, persisting data and creating relationships between data. Other subjects of interest to Core Data projects are NSPredicate, threading, and among others.
An example application of Core Data could a Catalog app for your local library. In the Catalog app a librarian could add or remove books. They could also filter books by genre, sort books by publication date, or search for a specific authors work. An entity “Book” would have various attributes such as title, author, publication date, isbn, call number, etc. Core Data including the above example can also store data gathered from a server.
Major components of the framework include:
Sources:
CoreData & Concurrency
It's important to remember that CoreData is NOT thread-safe, which means that if it's necessary to use for example a background-thread to work on ManagedObjects, there are new things to consider, like PrivateQueue- / MainQueue-ManagedObjectContexts.
From Apples documentary: Core Data expects to be run on a single thread. You should never share managed object contexts between threads. This is a hard rule you should not break.
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 {...}
lazy var managedObjectContext: NSManagedObjectContext = {...}
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
).
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
NSPersistentStoreCoordinator
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
.
NSManagedObjectContext
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
Attribute types include: Undefined, Integer 16, Integer 32, Integer 64, Decimal, Double, Float, String, Boolean, Date, Binary, Data, or Transformable
When defining an Entity
as abstract you won't be creating any instances of that entity. For example a Person would be abstract and a Employee or Customer would be a concrete subentities.
Transient
attributes are properties that you define as part of the model, but which are not saved to the persistent store as part of an entity instance’s data. Core Data does track changes you make to transient properties, so they are recorded for undo operations. You use transient properties for a variety of purposes, including keeping calculated values and derived values.
The Destination
field defines what object (or objects) are returned when the relationship is accessed in code.
The Inverse
field defines the other half of a relationship. Because each relationship is defined from one direction, this field joins two relationships together to create a fully bidirectional relationship.