SlideShare a Scribd company logo
1 of 23
Download to read offline
Core Data
Neo
Outline
Core Data Introduction
Core Data Stack
Core Data Operation
Core Data Concurrency
Experience
Reference:

https://developer.apple.com/library/watchos/documentation/Cocoa/Conceptual/
CoreData/index.html
Core Data Introduction
What is the Core Data
Core Data is not RDBS, it’s ORM
It’s a framework to manage the model layer object, not a DBMS
Intelligent and efficiency data management tools
Advantage in Core Data
Efficiency data r/w, ex:lazy loading, faulting pre-fetching, etc…
Intelligent data schema upgrade & migration
Schema version control
Data editing in memory, save changes back to store
Simple to know stack vs. RMDB
Core Data Stack
tables
table
configuration
file.sql
files
Core Data Stack
Establish a Core Data stack
Core Data Stack
Core Data Stack
Core Data Operation
Core Data Operation
Request data from fetchResultsController
Request data from executeFetchRequest
Core Data Operation
Insert new data to MOC
Delete data from MOC
Modify data in MOC
Save change from MOC to PSC
Core Data Operation
If you set delegate to fetchResultsController, then
you can monitor what change in the MOC
Core Data Concurrency
Core data supports concurrency operation and
easy to implement, there are two ways
Child Managed Object Context
Another Managed Object Context but same Persistent Store Coordinator
(Not recommend anymore)
What time does we need it
a lot of executions in core data, ex:insert, delete, or update
A complex query which need a long time
App is almost waiting for core data access
The advantage is performance improvement when
App has data access performance issue.
Core Data Concurrency
Shouldn’t easy to try it if no performance issue
Program would be more complex, and access data operation too.
Managed Object Context is not thread-safe
Data is needed to care about conflict and merge.
Data is may not synced when miss some operations
Core Data Concurrency
Select one concurrencyType when create MOC
NSConfinementConcurrencyType
Default type for backward, it’s for keeping MOC is accessed in the thread
which it is created. (Deprecated in iOS 9.0)
NSPrivateQueueConcurrencyType (child MOC)
Create a private queue by GCD, it won’t block mainThread.
NSMainQueueConcurrencyType (main MOC)
MOC is accessed in mainThread, it would block UI.
Core Data Concurrency
Child Concurrency architecture
Core Data Concurrency
Asynchronous Saving
When you have heavy I/O
in core data
Core Data Concurrency
Operation of child MOC is the same as parent
MOC, but there are something important
Pass the MO to different MOC that is forbidden
Child MOC save just update data of parent MOC, it won’t save data to
disk.
Before child MOC save, data of parent MOC won’t have any change,
also parent MOC can’t find data which belong to child MOC.
Is only the architecture?
NO, but I don’t think that need more concurrency architectures in the
most normal cases.
Experience
We got the Core Data fundamental now, but
something which need to be known in my experience
Experience
Pass MO between different contexts
We can’t pass MO directly
Use NSManagedObjectID to access MO from persistentStore or memory.
MO status maybe is not consistent

ex: MO is changed in child MOC without save, and the change won’t
affect the MO in parent MOC.
NSManagedObjectID has two status
PersistentID

The MO is saved back to persistent store, and can be accessed anywhere
by objectID
TemporaryID

The MO is no saved before, and it can’t be accessed from context.

After saved MO, you can’t find object by the ID!!

BTW, you can check the ID status by isTemporaryID
Experience
There are three methods access MO by objectID
objectWithID

Always return a MO, whatever it has data or not. 

It can accessed parent’s temp MO in child, but that’s a new one.

Don’t use return value as Bool, it always get a empty data MO!!
objectRegisteredForID

Return MO if existed in MOC, otherwise return nil. 

Ex: a MO saved in parent MOC, the method can’t access it in child
MOC.
existingObjectWithID

Return MO if it has been saved before, otherwise return nil.
Experience
MO has some status properties
isFault, isDeleted, faultingState, isInserted, isUpdated, isTemporaryID,
hasChanges
These status can be checked what they do in context
These status would be reset after saved
isDeleted
If delete MO and it’s no saved before, then the MO would be nil directly
If delete MO and it’s saved before, then only isDelete property would be changed

After deleted MO, it always existed if it was saved before!!
Check MO is deleted
Sometimes isDeleted can’t really know MO is deleted or not
Using existingObjectWithID and managedObjectContext

Check condition should be: (MO.isDelete || ![MOC exist..ID:MOID] || !
MO.managedObjectContext)
Experience
MO Update
Without save, change MO in the child MOC won’t affect attribute/status
MO in the parent MOC, otherwise, too.
Without save, insert MO in the child MOC won’t affect MO in the
parent MOC, otherwise, child MOC would be affected.
You can try core data operation in my testbed
https://github.com/flamelad/CoreDataTestbed
Q & A

More Related Content

Similar to Core Data Introduction

Mongo db with spring data document
Mongo db with spring data documentMongo db with spring data document
Mongo db with spring data document
Sean Lee
 

Similar to Core Data Introduction (20)

Entity Framework Core
Entity Framework CoreEntity Framework Core
Entity Framework Core
 
Entity Framework v2 Best Practices
Entity Framework v2 Best PracticesEntity Framework v2 Best Practices
Entity Framework v2 Best Practices
 
Core Data in Modern Times
Core Data in Modern TimesCore Data in Modern Times
Core Data in Modern Times
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
Core data ios10changes
Core data ios10changesCore data ios10changes
Core data ios10changes
 
The Django Book - Chapter 5: Models
The Django Book - Chapter 5: ModelsThe Django Book - Chapter 5: Models
The Django Book - Chapter 5: Models
 
Advanced Core Data
Advanced Core DataAdvanced Core Data
Advanced Core Data
 
Core data in Swfit
Core data in SwfitCore data in Swfit
Core data in Swfit
 
Data Binding in Silverlight
Data Binding in SilverlightData Binding in Silverlight
Data Binding in Silverlight
 
Elements for an iOS Backend
Elements for an iOS BackendElements for an iOS Backend
Elements for an iOS Backend
 
JDBC Part - 2
JDBC Part - 2JDBC Part - 2
JDBC Part - 2
 
MVVM
MVVMMVVM
MVVM
 
.NET Core, ASP.NET Core Course, Session 15
.NET Core, ASP.NET Core Course, Session 15.NET Core, ASP.NET Core Course, Session 15
.NET Core, ASP.NET Core Course, Session 15
 
Building a Testable Data Access Layer
Building a Testable Data Access LayerBuilding a Testable Data Access Layer
Building a Testable Data Access Layer
 
Mongo db with spring data document
Mongo db with spring data documentMongo db with spring data document
Mongo db with spring data document
 
How my team is applying JS framework for PHP projects.
How my team is applying JS framework for PHP projects.How my team is applying JS framework for PHP projects.
How my team is applying JS framework for PHP projects.
 
Dao pattern
Dao patternDao pattern
Dao pattern
 
Iphone programming: Core Data Tutorial for iOS
Iphone programming: Core Data Tutorial for iOSIphone programming: Core Data Tutorial for iOS
Iphone programming: Core Data Tutorial for iOS
 
Session 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaSession 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise Java
 
Spring Data MongoDB 介紹
Spring Data MongoDB 介紹Spring Data MongoDB 介紹
Spring Data MongoDB 介紹
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 

Core Data Introduction

  • 2. Outline Core Data Introduction Core Data Stack Core Data Operation Core Data Concurrency Experience Reference:
 https://developer.apple.com/library/watchos/documentation/Cocoa/Conceptual/ CoreData/index.html
  • 3. Core Data Introduction What is the Core Data Core Data is not RDBS, it’s ORM It’s a framework to manage the model layer object, not a DBMS Intelligent and efficiency data management tools Advantage in Core Data Efficiency data r/w, ex:lazy loading, faulting pre-fetching, etc… Intelligent data schema upgrade & migration Schema version control Data editing in memory, save changes back to store
  • 4. Simple to know stack vs. RMDB Core Data Stack tables table configuration file.sql files
  • 6. Establish a Core Data stack Core Data Stack
  • 9. Core Data Operation Request data from fetchResultsController Request data from executeFetchRequest
  • 10. Core Data Operation Insert new data to MOC Delete data from MOC Modify data in MOC Save change from MOC to PSC
  • 11. Core Data Operation If you set delegate to fetchResultsController, then you can monitor what change in the MOC
  • 12. Core Data Concurrency Core data supports concurrency operation and easy to implement, there are two ways Child Managed Object Context Another Managed Object Context but same Persistent Store Coordinator (Not recommend anymore) What time does we need it a lot of executions in core data, ex:insert, delete, or update A complex query which need a long time App is almost waiting for core data access The advantage is performance improvement when App has data access performance issue.
  • 13. Core Data Concurrency Shouldn’t easy to try it if no performance issue Program would be more complex, and access data operation too. Managed Object Context is not thread-safe Data is needed to care about conflict and merge. Data is may not synced when miss some operations
  • 14. Core Data Concurrency Select one concurrencyType when create MOC NSConfinementConcurrencyType Default type for backward, it’s for keeping MOC is accessed in the thread which it is created. (Deprecated in iOS 9.0) NSPrivateQueueConcurrencyType (child MOC) Create a private queue by GCD, it won’t block mainThread. NSMainQueueConcurrencyType (main MOC) MOC is accessed in mainThread, it would block UI.
  • 15. Core Data Concurrency Child Concurrency architecture
  • 16. Core Data Concurrency Asynchronous Saving When you have heavy I/O in core data
  • 17. Core Data Concurrency Operation of child MOC is the same as parent MOC, but there are something important Pass the MO to different MOC that is forbidden Child MOC save just update data of parent MOC, it won’t save data to disk. Before child MOC save, data of parent MOC won’t have any change, also parent MOC can’t find data which belong to child MOC. Is only the architecture? NO, but I don’t think that need more concurrency architectures in the most normal cases.
  • 18. Experience We got the Core Data fundamental now, but something which need to be known in my experience
  • 19. Experience Pass MO between different contexts We can’t pass MO directly Use NSManagedObjectID to access MO from persistentStore or memory. MO status maybe is not consistent
 ex: MO is changed in child MOC without save, and the change won’t affect the MO in parent MOC. NSManagedObjectID has two status PersistentID
 The MO is saved back to persistent store, and can be accessed anywhere by objectID TemporaryID
 The MO is no saved before, and it can’t be accessed from context.
 After saved MO, you can’t find object by the ID!!
 BTW, you can check the ID status by isTemporaryID
  • 20. Experience There are three methods access MO by objectID objectWithID
 Always return a MO, whatever it has data or not. 
 It can accessed parent’s temp MO in child, but that’s a new one.
 Don’t use return value as Bool, it always get a empty data MO!! objectRegisteredForID
 Return MO if existed in MOC, otherwise return nil. 
 Ex: a MO saved in parent MOC, the method can’t access it in child MOC. existingObjectWithID
 Return MO if it has been saved before, otherwise return nil.
  • 21. Experience MO has some status properties isFault, isDeleted, faultingState, isInserted, isUpdated, isTemporaryID, hasChanges These status can be checked what they do in context These status would be reset after saved isDeleted If delete MO and it’s no saved before, then the MO would be nil directly If delete MO and it’s saved before, then only isDelete property would be changed
 After deleted MO, it always existed if it was saved before!! Check MO is deleted Sometimes isDeleted can’t really know MO is deleted or not Using existingObjectWithID and managedObjectContext
 Check condition should be: (MO.isDelete || ![MOC exist..ID:MOID] || ! MO.managedObjectContext)
  • 22. Experience MO Update Without save, change MO in the child MOC won’t affect attribute/status MO in the parent MOC, otherwise, too. Without save, insert MO in the child MOC won’t affect MO in the parent MOC, otherwise, child MOC would be affected. You can try core data operation in my testbed https://github.com/flamelad/CoreDataTestbed
  • 23. Q & A