CORE DATA
WHAT IS CORE DATA?

•

data persistence framework	


•

not a database
CORE DATA COMPONENTS
User application
Managed object context
Managed 	

object

Managed 	

object

Managed 	

object

Persistent store coordinator
Persistent store
(SQLite, XML,	

in memory, 	

custom, …)

Managed	

object	

model
PERSISTENT STORE
•

NSPersistentStore	


•

Types:	

•

SQLite	


•

Binary	


•

In memory	


•

Custom
PERSISTENT STORE
COORDINATOR
•

NSPersistentStoreCoordinator	


•

associates persistent object stores and a managed
object model	


•

facade to managed object contexts	

•

group of persistent stores appears as a single
aggregate store
PERSISTENT STORE
COORDINATOR
•

Complex application
MOC

MOC

MOC

Persistent store coordinator

Storage1

Storage2

Storage3

Storage4
MANAGED OBJECT MODEL
(MOM)
•

A collection of entity descriptions	

•

name of the class, properties (attributes and relationships)
MANAGED OBJECT CONTEXT
(MOC)
•

NSManagedObjectContext	


•

environment where managed objects live	


•

responsibility to manage a collection of managed objects 	

•

validation	


•

faulting	


•

(inverse) relationship handling 	


•

undo/redo	

!

•

should be always accessible
MANAGED OBJECT
•

NSManagedObject	


•

model object	

•

represents a record from persistent store
Entity
description

Managed object - Person
dateOfBirth
07.03.1990.	

firstName
Ivan
lastName
Horvat
CORE DATA PROJECT
•

INITIALISATION	

•
•

managed object model	


•
•

persistent store coordinator	

managed object context	


HOW?	

•

use Xcode template
CORE DATA PROJECT
•

New project - Master-Detail Application
INSERTING OBJECT
NSManagedObjectContext *context = …

!

CDLocation *location = [NSEntityDescription
insertNewObjectForEntityForName:@“CDLocation”
inManagedObjectContext:context];

!

.
.
.

!

NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
NSLog(@“Error: %@“, error);
}
CONTEXT HIERARCHY
•

child - parent
MOC

MOC
MOC

MOC

MOC

MOC

MOC

MOC

Persistent store coordinator
TEMPORARY OBJECTS
•

!

child context:
NSManagedObjectContext *context = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSMainQueueConcurrencyType];
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
context.parentContext = self.managedObjectContext;
// self.managedObjectContext is main context
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription
insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
.
.
.
TEMPORARY OBJECTS
•

[context save:&error] will save object to main
context (parent context)	

•

object is still not saved to persistent store	


•

remove object from main context: undo or rollback	


•

save object in persistent store: save main context
UNDO/REDO/ROLLBACK
•

NSUndoManager	

[context setUndoManager:undoManager];

!
!

•

[context undo];
[context redo];

undo all unsaved changes	

[context rollback];
FETCHING OBJECTS
•

NSFetchRequest

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@“CDLocation"
inManagedObjectContext:context];

!

[request setEntity:entity];
NSArray *locations = [context executeFetchRequest:request
error:nil];
SORT RESULTS
•

NSSortDescriptor

NSFetchRequest *request = [[NSFetchRequest alloc] init];
…
…
…
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey: @"dateOfBirth" ascending:NO];
[request setSortDescriptors:@[sortDescriptor]];
!
NSArray *results = [context executeFetchRequest:request
error:nil];
FILTER RESULTS
•

NSPredicate

NSFetchRequest *request = [[NSFetchRequest alloc] init];
…
…
…
NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"firstName contains %@", @"Ivana"];
[request setPredicate:predicate];
!
NSArray *results = [context executeFetchRequest:request
error:nil];
SEE ALSO
•

NSFetchedResultController	


•

NSIncrementalStore	


•

Custom storage types	


•

Migrating data and versioning
LITERATURE
•

Apple documentation

Core Data programming Guide: https://developer.apple.com/library/mac/documentation/
cocoa/Conceptual/CoreData/cdProgrammingGuide.html

Core Data Core Competencies: https://developer.apple.com/library/ios/documentation/
DataManagement/Devpedia-CoreData/coreDataOverview.html

	


•

Pro Core Data for iOS - second edition

Michael Privat, Robert Warner

Infinum iOS Talks S01E02 - Things every iOS developer should know about Core Data by Filip Beć