SlideShare a Scribd company logo
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

More Related Content

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

Advanced Hibernate V2
Advanced Hibernate V2Advanced Hibernate V2
Advanced Hibernate V2
Haitham Raik
 
Advanced Apache Cayenne
Advanced Apache CayenneAdvanced Apache Cayenne
Advanced Apache Cayenne
WO Community
 

Similar to Infinum iOS Talks S01E02 - Things every iOS developer should know about Core Data by Filip Beć (20)

Connecting to a REST API in iOS
Connecting to a REST API in iOSConnecting to a REST API in iOS
Connecting to a REST API in iOS
 
Core Data Migration
Core Data MigrationCore Data Migration
Core Data Migration
 
CoreData
CoreDataCoreData
CoreData
 
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
 
Core data orlando i os dev group
Core data   orlando i os dev groupCore data   orlando i os dev group
Core data orlando i os dev group
 
IBM File Net P8
IBM File Net P8IBM File Net P8
IBM File Net P8
 
Advanced Hibernate V2
Advanced Hibernate V2Advanced Hibernate V2
Advanced Hibernate V2
 
Core Data with Swift 3.0
Core Data with Swift 3.0Core Data with Swift 3.0
Core Data with Swift 3.0
 
Advanced Apache Cayenne
Advanced Apache CayenneAdvanced Apache Cayenne
Advanced Apache Cayenne
 
Mule object store
Mule  object storeMule  object store
Mule object store
 
Data perisistence in iOS
Data perisistence in iOSData perisistence in iOS
Data perisistence in iOS
 
Data perisistance i_os
Data perisistance i_osData perisistance i_os
Data perisistance i_os
 
Object store
Object storeObject store
Object store
 
Effiziente persistierung
Effiziente persistierungEffiziente persistierung
Effiziente persistierung
 
Accelerating Rails with edge caching
Accelerating Rails with edge cachingAccelerating Rails with edge caching
Accelerating Rails with edge caching
 
Core data in Swfit
Core data in SwfitCore data in Swfit
Core data in Swfit
 
Core data optimization
Core data optimizationCore data optimization
Core data optimization
 
Create a Core Data Observer in 10mins
Create a Core Data Observer in 10minsCreate a Core Data Observer in 10mins
Create a Core Data Observer in 10mins
 
Rails Caching: Secrets From the Edge
Rails Caching: Secrets From the EdgeRails Caching: Secrets From the Edge
Rails Caching: Secrets From the Edge
 
Rails Caching Secrets from the Edge
Rails Caching Secrets from the EdgeRails Caching Secrets from the Edge
Rails Caching Secrets from the Edge
 

Recently uploaded

Recently uploaded (20)

Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 

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