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

Pers...
PERSISTENT STORE
•

NSPersistentStore	


•

Types:	

•

SQLite	


•

Binary	


•

In memory	


•

Custom
PERSISTENT STORE
COORDINATOR
•

NSPersistentStoreCoordinator	


•

associates persistent object stores and a managed
objec...
PERSISTENT STORE
COORDINATOR
•

Complex application
MOC

MOC

MOC

Persistent store coordinator

Storage1

Storage2

Stora...
MANAGED OBJECT MODEL
(MOM)
•

A collection of entity descriptions	

•

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

NSManagedObjectContext	


•

environment where managed objects live	


•

responsibility t...
MANAGED OBJECT
•

NSManagedObject	


•

model object	

•

represents a record from persistent store
Entity
description

Ma...
CORE DATA PROJECT
•

INITIALISATION	

•
•

managed object model	


•
•

persistent store coordinator	

managed object cont...
CORE DATA PROJECT
•

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

!

CDLocation *location = [NSEntityDescription
insertNewObjectForEnt...
CONTEXT HIERARCHY
•

child - parent
MOC

MOC
MOC

MOC

MOC

MOC

MOC

MOC

Persistent store coordinator
TEMPORARY OBJECTS
•

!

child context:
NSManagedObjectContext *context = [[NSManagedObjectContext alloc]
initWithConcurren...
TEMPORARY OBJECTS
•

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

•

object is still not save...
UNDO/REDO/ROLLBACK
•

NSUndoManager	

[context setUndoManager:undoManager];

!
!

•

[context undo];
[context redo];

undo...
FETCHING OBJECTS
•

NSFetchRequest

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity =...
SORT RESULTS
•

NSSortDescriptor

NSFetchRequest *request = [[NSFetchRequest alloc] init];
…
…
…
NSSortDescriptor *sortDes...
FILTER RESULTS
•

NSPredicate

NSFetchRequest *request = [[NSFetchRequest alloc] init];
…
…
…
NSPredicate *predicate = [NS...
SEE ALSO
•

NSFetchedResultController	


•

NSIncrementalStore	


•

Custom storage types	


•

Migrating data and version...
LITERATURE
•

Apple documentation

Core Data programming Guide: https://developer.apple.com/library/mac/documentation/
coc...
Upcoming SlideShare
Loading in …5
×

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

877 views

Published on

Learn about persistent store coordinator, managed object model, object contexts and other wonderful things in the Core Data iOS framework.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
877
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
11
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

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

  1. 1. CORE DATA
  2. 2. WHAT IS CORE DATA? • data persistence framework • not a database
  3. 3. 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
  4. 4. PERSISTENT STORE • NSPersistentStore • Types: • SQLite • Binary • In memory • Custom
  5. 5. 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
  6. 6. PERSISTENT STORE COORDINATOR • Complex application MOC MOC MOC Persistent store coordinator Storage1 Storage2 Storage3 Storage4
  7. 7. MANAGED OBJECT MODEL (MOM) • A collection of entity descriptions • name of the class, properties (attributes and relationships)
  8. 8. 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
  9. 9. MANAGED OBJECT • NSManagedObject • model object • represents a record from persistent store Entity description Managed object - Person dateOfBirth 07.03.1990. firstName Ivan lastName Horvat
  10. 10. CORE DATA PROJECT • INITIALISATION • • managed object model • • persistent store coordinator managed object context HOW? • use Xcode template
  11. 11. CORE DATA PROJECT • New project - Master-Detail Application
  12. 12. 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); }
  13. 13. CONTEXT HIERARCHY • child - parent MOC MOC MOC MOC MOC MOC MOC MOC Persistent store coordinator
  14. 14. 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]; . . .
  15. 15. 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
  16. 16. UNDO/REDO/ROLLBACK • NSUndoManager [context setUndoManager:undoManager]; ! ! • [context undo]; [context redo]; undo all unsaved changes [context rollback];
  17. 17. 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];
  18. 18. 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];
  19. 19. 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];
  20. 20. SEE ALSO • NSFetchedResultController • NSIncrementalStore • Custom storage types • Migrating data and versioning
  21. 21. 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

×