Core Data Reference

Kip Landergren

(Updated: )

Contents

Terminology

stack the core data classes necessary to manage and persist your application’s data layer
container unified interface for creation, management, and access of a single core data stack
object model / model the structure of your object graph data
context work area for interacting with objects
coordinator traffic cop between contexts, stores, and models
store / persistent store maintainer of data (when persistent, on disk) over time
managed object a realized instance of an entity defined by your model, specific to a context
entity data structure of an object within the object graph. AKA a database table
attribute a named property with an associated value
relationship a named property with an associated object (or objects)
fetch to retrieve data from a persistent store
fetch request search criteria for a query to a persistent store
fetched results controller executes a fetch request and manages results
fault placeholder object in the persistent store; a promise of a future object
fire a fault / faulted / faulting realizing an object into memory, typically when the instance variable of a fault is accessed; should be transparent

Class and Protocol Overview

Core Data Stack

NSPersistentContainer

Holds all Core Data stack objects. Provides convenient access to viewContext and the ability to create new background contexts / do background work.

Should be subclassed—even if just an empty class—to better find the NSManagedObjectModel describing your object graph.

NSPersistentContainer - Core Data | Apple Developer Documentation

NSManagedObjectModel

Describes the structure of your object graph data in a way that is understood by an NSPersistentStore. Managed via Xcode’s data modeling tool and stored on disk in your .xcdatamodeld file.

NSManagedObjectModel - Core Data | Apple Developer Documentation

NSManagedObjectContext

Intelligent scratch pad for access and management of NSManagedObject instances. Keeps changes in memory until saved to an NSPersistentStore. Objects must be saved for persistence to work.

NSManagedObjectContext - Core Data | Apple Developer Documentation

NSPersistentStoreCoordinator

Mediates and facilitates the communication between NSManagedObjectContext instances and NSPersistentStore instances by controlling management of the NSManagedObjectModel.

A coordinator is able to compare the app’s managed object model with the store’s version and automatically migrate it forward.

NSPersistentStoreCoordinator - Core Data | Apple Developer Documentation

NSPersistentStore

Abstract base class that maintains data over time. Concrete types include:

NSPersistentStore - Core Data | Apple Developer Documentation

Core Data Model

NSManagedObject

Container object for a realized NSEntityDescription, associated with a single NSManagedObjectContext.

NSManagedObject - Core Data | Apple Developer Documentation

NSEntityDescription

Describes the metadata of an entity managed by Core Data. Can be thought of like a data base table.

NSEntityDescription - Core Data | Apple Developer Documentation

NSPropertyDescription

Describes a single property of an entity managed by Core Data. Subclasses include:

Note: special care should be taken to not choose names that overlap with existing no-parameter methods. The class documentation gives a good explanation of this.

NSPropertyDescription - Core Data | Apple Developer Documentation

Core Data Querying

NSFetchRequest

Describes the search criteria for retrieving data from a persistent store.

NSFetchRequest - Core Data | Apple Developer Documentation

NSFetchedResultsController

Manages the results of an NSFetchRequest and provides convenient functionality for integrating with UITableViewDataSource and UITableViewDelegate.

NSFetchedResultsController - Core Data | Apple Developer Documentation

NSFetchedResultsControllerDelegate

Protocol for communicating data changes encountered by the associated NSFetchedResultsController.

NSFetchedResultsControllerDelegate - Core Data | Apple Developer Documentation

Core Data Batch Operations

NSBatchDeleteRequest

Core Data typically loads data into memory to perform operations on it. This object will execute a delete request directly on the store, bypassing normal channels and the need to load data into memory.

Important note: active contexts are not automatically made aware of a batch delete request—take special care to review that everything works as expected!

NSBatchDeleteRequest - Core Data | Apple Developer Documentation

Creating an Object Model

Important notes:

CodeGen

Three options:

When you select extension you are responsible for creating the managed object subclass. Refer to the generated class to see the structure needed.

@NSManaged

Attribute that tells the swift compiler that Core Data will provide storage and declaration at run time.

Working with Stores

Note: deleting a store does not notify NSFetchedResultsViewController instances.

NSPersistentContainer will create Library/Application Support for you and initiatlize your .sqlite store there. Unknown at the moment if it is configurable, but I suspect it is.

Working with Data

Remember: a context always has an internally consistent object graph, but that does not mean it necessarily agrees with the persistent store on disk. Multiple contexts can exist, leading to conflict between what is “truth”. Review merge policy and locking for more info.

NSExpression

Important:

NSExpressionDescription

Combine an expression, name, and result type to generate a virtual property in propertiesToFetch.

Note:

NSPredicate

Generally: put the most restrictive predicate first for better performance.

// fetch a specific relationship with NSPredicate
let predicate = NSPredicate(format: "foo == %@", foo.objectID)

NSFetchRequest

Consider:

NSFetchedResultsController

Tips

Gotchas

NSFetchedResultsControllerDelegate

Tips

Prevent NSInternalInconsistencyException by not relying solely on controllerDidChangeContent.

Instead make use of:

Gotchas

controller(_:didChange:at:for:newIndexPath:) has special rules based on the NSFetchedResultsChangeType :

Working with Managed Objects

Important

Conventions

Init

// creating a managed object
let bar = BarMO(context: context)

Accessing Properties

Generalized approach:

NSManagedObjectContext

Best Practices

Format Strings

%d signed 32-bit integer
%@ object

More info in this Stack Overflow question: “What are the supported Swift String format specifiers?”.

Resources

Documentation

WWDC Talks