Core Data is Apple's framework for managing and persisting data in iOS and macOS applications. It provides objects for managing data models (NSManagedObjectModel), object contexts (NSManagedObjectContext), and connections to persistent stores like SQLite (NSPersistentStoreCoordinator, NSPersistentStore). Core Data graphs and saves managed objects, handling all the complexities of object relationships and concurrency. Developers can customize data storage through entities, attributes, relationships in the data model and by choosing XML, SQLite, binary, or in-memory storage formats.
5. Core Data Layers
Application
NSManagedObject
NSManagedObjectContext
NSPersistentStoreCoordinator NSManagedObjectModel
NSPersistentStore
XML SQLite Binary In Memory Custom
6. Persistent Store
NSPersistentStore
Four storage types: XML (Mac only), SQLite, binary,
and in memory
Developers can add their own custom formats
7. Persistent Store Coordinator
NSPersistentStoreCoordinator
Façade between persistent store(s) and managed
object context(s)
May only have one MOM
Not thread safe!
lock, unlock, and tryLock methods are provided.
11. Entities
NSEntityDescription
Entities represent objects
Entities can have attributes such as numbers and
strings
Entities can have to-one and to-many relationships to
other entities
12. Managed Object Context
NSManagedObjectContext
The “scratch pad” of you application
Changes aren’t saved until calling save:
Always check for errors
Create a new context for each thread
Commonly called a “MOC”
16. Fetch Request Efficiency Jedi
Level
Smaller batches may be requested for greater
efficiency (SQLite only)
Objects may be returned as faults
SUBQUERY can be used to have the database do the
work instead of Core Data
17. Fetch Request Templates Jedi
Level
Common fetch requests can be stored as a template
For example, all persons with a given name
Use fetchRequestFromTemplateWithName:
substitutionVariables:
18. Transient Attributes Jedi
Level
Attributes in the local object but not in the store
Typically used for convenience
Example: fullName = “firstName lastName”
Requires adding code to generated objects
19. Delete Rules Jedi
Level
Deny: Don’t allow if some relation exists
Nullify: Set relationship to nil
Cascade: Delete related objects
No Action: Do nothing, I want to cleanup
20. Versioning and Migration Jedi
Level
MOM is your contract with Core Data
If you change your model you must migrate the data to
match the new data model
Use a custom mapping model for changes
Lightweight migration automatically makes minor
changes (Snow Leopard/iOS 3.0+)
21. Configurations Jedi
Level
A persistent store coordinator may only have one MOM
One MOM may have many configurations
A configuration is a subset of entities
Typically used to store different entities in different
stores using one store coordinator