SlideShare a Scribd company logo
Core Data
By Robert Brown
@robby_brown
Basic Core Data
What is Core Data?


Intimidating
An amazing data persistence framework
Not a database
Model View Controller

 Interface
             View             Controller   Xcode
  Builder



                      Model


                    Core Data
Core Data Layers

                           Application

                        NSManagedObject

                   NSManagedObjectContext

     NSPersistentStoreCoordinator    NSManagedObjectModel

                        NSPersistentStore

      XML      SQLite      Binary    In Memory   Custom
Persistent Store


 NSPersistentStore
 Four storage types: XML (Mac only), SQLite, binary,
 and in memory
 Developers can add their own custom formats
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.
Persistent Store Coordinator

Managed
  Object
Contexts
 Persistent
   Store
Coordinator
Persistent
 Stores
Managed Object
Model

NSManagedObjectModel
A representation of the app’s data model
Visually built in Xcode (usually)
Commonly called a “MOM”
Managed Object
Model
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
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”
Managed Object

NSManagedObject
Superclass of all managed objects
Properties can be accessed via KVC/KVO
Don’t pass managed objects between threads, pass
their objectID instead
Fetch Requests


NSFetchRequest
Used to retrieve objects from the store
Uses an NSPredicate for the query
Advanced Core Data
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
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:
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
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
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+)
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
Demo
Resources


Apple Documentation
Core Data: Apple’s API for Persisting Data on Mac OS
X by Marcus S. Zarra
https://developer.apple.com/videos/wwdc/2010/
Questions?

More Related Content

Similar to Core Data

Hibernate 3
Hibernate 3Hibernate 3
Hibernate 3
Rajiv Gupta
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)
David McCarter
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)
David McCarter
 
Mac/iOS Design Patterns
Mac/iOS Design PatternsMac/iOS Design Patterns
Mac/iOS Design Patterns
Robert Brown
 
Intro to Core Data
Intro to Core DataIntro to Core Data
Intro to Core Data
Make School
 
Entity Framework Today (May 2012)
Entity Framework Today (May 2012)Entity Framework Today (May 2012)
Entity Framework Today (May 2012)
Julie Lerman
 
Getting started with entity framework
Getting started with entity framework Getting started with entity framework
Getting started with entity framework
Lushanthan Sivaneasharajah
 
Free Hibernate Tutorial | VirtualNuggets
Free Hibernate Tutorial  | VirtualNuggetsFree Hibernate Tutorial  | VirtualNuggets
Free Hibernate Tutorial | VirtualNuggets
Virtual Nuggets
 
Ef code first
Ef code firstEf code first
Ef code first
ZealousysDev
 
MVVM
MVVMMVVM
Ado.net & data persistence frameworks
Ado.net & data persistence frameworksAdo.net & data persistence frameworks
Ado.net & data persistence frameworks
Luis Goldster
 
Object- Relational Persistence in Smalltalk
Object- Relational Persistence in SmalltalkObject- Relational Persistence in Smalltalk
Object- Relational Persistence in Smalltalk
ESUG
 
Hibernate Session 1
Hibernate Session 1Hibernate Session 1
Hibernate Session 1
b_kathir
 
EnScript Workshop
EnScript WorkshopEnScript Workshop
EnScript Workshop
Mark Morgan, CCE, EnCE
 
La sql
La sqlLa sql
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
James Johnson
 
Hibernate
HibernateHibernate
Core data in Swfit
Core data in SwfitCore data in Swfit
Core data in Swfit
allanh0526
 
Introduction to odbms
Introduction to odbmsIntroduction to odbms
Introduction to odbms
ajay pashankar
 
.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13
aminmesbahi
 

Similar to Core Data (20)

Hibernate 3
Hibernate 3Hibernate 3
Hibernate 3
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)
 
Mac/iOS Design Patterns
Mac/iOS Design PatternsMac/iOS Design Patterns
Mac/iOS Design Patterns
 
Intro to Core Data
Intro to Core DataIntro to Core Data
Intro to Core Data
 
Entity Framework Today (May 2012)
Entity Framework Today (May 2012)Entity Framework Today (May 2012)
Entity Framework Today (May 2012)
 
Getting started with entity framework
Getting started with entity framework Getting started with entity framework
Getting started with entity framework
 
Free Hibernate Tutorial | VirtualNuggets
Free Hibernate Tutorial  | VirtualNuggetsFree Hibernate Tutorial  | VirtualNuggets
Free Hibernate Tutorial | VirtualNuggets
 
Ef code first
Ef code firstEf code first
Ef code first
 
MVVM
MVVMMVVM
MVVM
 
Ado.net & data persistence frameworks
Ado.net & data persistence frameworksAdo.net & data persistence frameworks
Ado.net & data persistence frameworks
 
Object- Relational Persistence in Smalltalk
Object- Relational Persistence in SmalltalkObject- Relational Persistence in Smalltalk
Object- Relational Persistence in Smalltalk
 
Hibernate Session 1
Hibernate Session 1Hibernate Session 1
Hibernate Session 1
 
EnScript Workshop
EnScript WorkshopEnScript Workshop
EnScript Workshop
 
La sql
La sqlLa sql
La sql
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
 
Hibernate
HibernateHibernate
Hibernate
 
Core data in Swfit
Core data in SwfitCore data in Swfit
Core data in Swfit
 
Introduction to odbms
Introduction to odbmsIntroduction to odbms
Introduction to odbms
 
.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 13
 

More from Robert Brown

High level concurrency
High level concurrencyHigh level concurrency
High level concurrency
Robert Brown
 
Data Source Combinators
Data Source CombinatorsData Source Combinators
Data Source Combinators
Robert Brown
 
Elixir
ElixirElixir
Elixir
Robert Brown
 
Reactive Cocoa
Reactive CocoaReactive Cocoa
Reactive Cocoa
Robert Brown
 
UIKit Dynamics
UIKit DynamicsUIKit Dynamics
UIKit Dynamics
Robert Brown
 
iOS State Preservation and Restoration
iOS State Preservation and RestorationiOS State Preservation and Restoration
iOS State Preservation and Restoration
Robert Brown
 
Anti-Patterns
Anti-PatternsAnti-Patterns
Anti-Patterns
Robert Brown
 
Pragmatic blocks
Pragmatic blocksPragmatic blocks
Pragmatic blocks
Robert Brown
 
Automatic Reference Counting
Automatic Reference CountingAutomatic Reference Counting
Automatic Reference Counting
Robert Brown
 
Grand Central Dispatch Design Patterns
Grand Central Dispatch Design PatternsGrand Central Dispatch Design Patterns
Grand Central Dispatch Design Patterns
Robert Brown
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatch
Robert Brown
 
Quick Look for iOS
Quick Look for iOSQuick Look for iOS
Quick Look for iOS
Robert Brown
 

More from Robert Brown (12)

High level concurrency
High level concurrencyHigh level concurrency
High level concurrency
 
Data Source Combinators
Data Source CombinatorsData Source Combinators
Data Source Combinators
 
Elixir
ElixirElixir
Elixir
 
Reactive Cocoa
Reactive CocoaReactive Cocoa
Reactive Cocoa
 
UIKit Dynamics
UIKit DynamicsUIKit Dynamics
UIKit Dynamics
 
iOS State Preservation and Restoration
iOS State Preservation and RestorationiOS State Preservation and Restoration
iOS State Preservation and Restoration
 
Anti-Patterns
Anti-PatternsAnti-Patterns
Anti-Patterns
 
Pragmatic blocks
Pragmatic blocksPragmatic blocks
Pragmatic blocks
 
Automatic Reference Counting
Automatic Reference CountingAutomatic Reference Counting
Automatic Reference Counting
 
Grand Central Dispatch Design Patterns
Grand Central Dispatch Design PatternsGrand Central Dispatch Design Patterns
Grand Central Dispatch Design Patterns
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatch
 
Quick Look for iOS
Quick Look for iOSQuick Look for iOS
Quick Look for iOS
 

Recently uploaded

Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Precisely
 

Recently uploaded (20)

Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
 

Core Data

  • 1. Core Data By Robert Brown @robby_brown
  • 3. What is Core Data? Intimidating An amazing data persistence framework Not a database
  • 4. Model View Controller Interface View Controller Xcode Builder Model Core Data
  • 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.
  • 8. Persistent Store Coordinator Managed Object Contexts Persistent Store Coordinator Persistent Stores
  • 9. Managed Object Model NSManagedObjectModel A representation of the app’s data model Visually built in Xcode (usually) Commonly called a “MOM”
  • 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”
  • 13. Managed Object NSManagedObject Superclass of all managed objects Properties can be accessed via KVC/KVO Don’t pass managed objects between threads, pass their objectID instead
  • 14. Fetch Requests NSFetchRequest Used to retrieve objects from the store Uses an NSPredicate for the query
  • 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
  • 22. Demo
  • 23. Resources Apple Documentation Core Data: Apple’s API for Persisting Data on Mac OS X by Marcus S. Zarra https://developer.apple.com/videos/wwdc/2010/

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n