SlideShare a Scribd company logo
Build your own
Core Data Observer
Zach McArtor, @zmcartor, http://hackazach.net
Wat?
• Small component that notifies us of
NSManagedObject changes/saves
• React to those changes via method invocation
Why?
• Sometimes, an NSFetchedResultsController
won’t cut it
• Want to update a tiny piece of UI or run a
background action
• Be reactive!
Relevant Notifications
Fired by NSManagedObjectContext
• NSManagedObjectContextObjectsDidChangeNotification
• NSManagedObjectContextDidSaveNotification
Subscribe, and ye shall receive
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:SomeManagedObjectContext];
An NSDict of Changes!
• NSDict keyed by these constants:
• { NSInsertedObjectsKey,
• NSUpdatedObjectsKey
• NSDeletedObjectsKey. }
Last step, add a predicate
• Subscribing to didChange/DidSave triggers a
callback for every model save/change. We only
care about a subset of models.
• Abstract notification subscription into Class
• Predicate filter models returned in NSDict. (Protip!)
Dealing Concurrency With
• There are different ways to set up a concurrent
Core Data stack :(
!
!
!
http://floriankugler.com/blog/2013/4/2/theconcurrentcoredatastack
https://www.cocoanetics.com/2012/07/multi-context-coredata/
Which context to subscribe?
• ‘didSave’ notifications fired during context save
(duh.) Going up chain child->parent
• ’didChange’ notifications fired when context
merges changes (MagicalRecord merges down into default context)
Watch out!
• The managedObjects received via notification
may not be in the correct context for use on
main thread.
• Use existingObjectWithId:error: to pull into
correct context
• Subscribe to a single context, don’t listen for
every event within a nested system
Thanks for
observing this
talk!
Reference implementation :
https://github.com/mzarra/ZDS_Shared/blob/master/
ZSContextWatcher.m

More Related Content

PDF
Core Data with multiple managed object contexts
Matthew Morey
 
PDF
High Performance Core Data
Matthew Morey
 
PDF
Adventures in Multithreaded Core Data
Inferis
 
KEY
iOSDevCamp 2011 Core Data
Chris Mar
 
PDF
Multi-threaded CoreData Done Right
morrowa_de
 
PPT
Core data orlando i os dev group
Andrew Kozlik
 
PDF
Modern Android app library stack
Tomáš Kypta
 
ODP
Indexed db
Martin Giger
 
Core Data with multiple managed object contexts
Matthew Morey
 
High Performance Core Data
Matthew Morey
 
Adventures in Multithreaded Core Data
Inferis
 
iOSDevCamp 2011 Core Data
Chris Mar
 
Multi-threaded CoreData Done Right
morrowa_de
 
Core data orlando i os dev group
Andrew Kozlik
 
Modern Android app library stack
Tomáš Kypta
 
Indexed db
Martin Giger
 

What's hot (20)

PDF
Simpler Core Data with RubyMotion
Stefan Haflidason
 
TXT
Birhanu distributive assignment
university
 
PPTX
Core Data Performance Guide Line
Gagan Vishal Mishra
 
PPTX
SenchaCon 2016: The Once and Future Grid - Nige White
Sencha
 
PDF
ERGroupware
WO Community
 
PPT
Core Data Migration
Monica Kurup
 
PDF
iOS for ERREST - alternative version
WO Community
 
PPTX
Whats New for WPF in .NET 4.5
Rainer Stropek
 
PPTX
Angular JS deep dive
Axilis
 
PDF
[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules
Srijan Technologies
 
ZIP
Easy undo.key
zachwaugh
 
PDF
Александр Терещук - Memory Analyzer Tool and memory optimization tips in Android
UA Mobile
 
PDF
Realm.io par Clement Sauvage
CocoaHeads France
 
ODP
Polyglot persistence with Spring Data
Corneil du Plessis
 
PPTX
Creating Single Page Web App using Backbone JS
Akshay Mathur
 
PPTX
Redux + RxJS + Ember makes simple
Justin Park
 
PDF
MongoDB Performance Tuning
MongoDB
 
PPTX
Dbabstraction
Bruce McPherson
 
PPT
Zend framework 03 - singleton factory data mapper caching logging
Tricode (part of Dept)
 
PPTX
Google apps script database abstraction exposed version
Bruce McPherson
 
Simpler Core Data with RubyMotion
Stefan Haflidason
 
Birhanu distributive assignment
university
 
Core Data Performance Guide Line
Gagan Vishal Mishra
 
SenchaCon 2016: The Once and Future Grid - Nige White
Sencha
 
ERGroupware
WO Community
 
Core Data Migration
Monica Kurup
 
iOS for ERREST - alternative version
WO Community
 
Whats New for WPF in .NET 4.5
Rainer Stropek
 
Angular JS deep dive
Axilis
 
[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules
Srijan Technologies
 
Easy undo.key
zachwaugh
 
Александр Терещук - Memory Analyzer Tool and memory optimization tips in Android
UA Mobile
 
Realm.io par Clement Sauvage
CocoaHeads France
 
Polyglot persistence with Spring Data
Corneil du Plessis
 
Creating Single Page Web App using Backbone JS
Akshay Mathur
 
Redux + RxJS + Ember makes simple
Justin Park
 
MongoDB Performance Tuning
MongoDB
 
Dbabstraction
Bruce McPherson
 
Zend framework 03 - singleton factory data mapper caching logging
Tricode (part of Dept)
 
Google apps script database abstraction exposed version
Bruce McPherson
 
Ad

Similar to Create a Core Data Observer in 10mins (20)

ODP
MobileCity:Core Data
Allan Davis
 
PDF
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
smn-automate
 
PDF
Intro to Core Data
Make School
 
PPTX
Change tracking
Sonny56
 
PDF
Elements for an iOS Backend
Laurent Cerveau
 
PPTX
SenchaCon 2016: Ext JS + React: A Match Made in UX Heaven - Mark Brocato
Sencha
 
PDF
Advanced Cassandra
DataStax Academy
 
PPT
Connecting to a REST API in iOS
gillygize
 
PPTX
CQRS and Event Sourcing
Sergey Seletsky
 
PDF
An introduction to React.js
Emanuele DelBono
 
PPTX
React JS .NET
Jennifer Estrada
 
PDF
Core Data with Swift 3.0
Korhan Bircan
 
PDF
WOdka
WO Community
 
PDF
vBrownBag 2015-07-08 - VCP6-DCV Foundations Section 7: Monitoring
Dan Barr
 
PPTX
Using the Tooling API to Generate Apex SOAP Web Service Clients
Daniel Ballinger
 
PPTX
Managing state in asp.net
Sireesh K
 
PDF
Using redux and angular 2 with meteor
Ken Ono
 
PDF
Using redux and angular 2 with meteor
Ken Ono
 
PDF
VMworld 2013: Extreme Performance Series: vCenter of the Universe
VMworld
 
KEY
DjangoCon 2010 Scaling Disqus
zeeg
 
MobileCity:Core Data
Allan Davis
 
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
smn-automate
 
Intro to Core Data
Make School
 
Change tracking
Sonny56
 
Elements for an iOS Backend
Laurent Cerveau
 
SenchaCon 2016: Ext JS + React: A Match Made in UX Heaven - Mark Brocato
Sencha
 
Advanced Cassandra
DataStax Academy
 
Connecting to a REST API in iOS
gillygize
 
CQRS and Event Sourcing
Sergey Seletsky
 
An introduction to React.js
Emanuele DelBono
 
React JS .NET
Jennifer Estrada
 
Core Data with Swift 3.0
Korhan Bircan
 
vBrownBag 2015-07-08 - VCP6-DCV Foundations Section 7: Monitoring
Dan Barr
 
Using the Tooling API to Generate Apex SOAP Web Service Clients
Daniel Ballinger
 
Managing state in asp.net
Sireesh K
 
Using redux and angular 2 with meteor
Ken Ono
 
Using redux and angular 2 with meteor
Ken Ono
 
VMworld 2013: Extreme Performance Series: vCenter of the Universe
VMworld
 
DjangoCon 2010 Scaling Disqus
zeeg
 
Ad

Recently uploaded (20)

PDF
Cybersecurity Awareness Presentation ppt.
banodhaharshita
 
PDF
BGP Security Best Practices that Matter, presented at PHNOG 2025
APNIC
 
PPTX
Pengenalan perangkat Jaringan komputer pada teknik jaringan komputer dan tele...
Prayudha3
 
PPTX
The Internet of Things (IoT) refers to a vast network of interconnected devic...
chethana8182
 
PDF
LB# 820-1889_051-7370_C000.schematic.pdf
matheusalbuquerqueco3
 
PPTX
谢尔丹学院毕业证购买|Sheridan文凭不见了怎么办谢尔丹学院成绩单
mookxk3
 
PPTX
dns domain name system history work.pptx
MUHAMMADKAVISHSHABAN
 
PPT
Introduction to dns domain name syst.ppt
MUHAMMADKAVISHSHABAN
 
PPTX
The Internet of Things (IoT) refers to a vast network of interconnected devic...
chethana8182
 
PPTX
AI ad its imp i military life read it ag
ShwetaBharti31
 
PDF
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 
PPTX
办理方法西班牙假毕业证蒙德拉贡大学成绩单MULetter文凭样本
xxxihn4u
 
PPT
Transformaciones de las funciones elementales.ppt
rirosel211
 
PDF
Latest Scam Shocking the USA in 2025.pdf
onlinescamreport4
 
PPTX
Perkembangan Perangkat jaringan komputer dan telekomunikasi 3.pptx
Prayudha3
 
PPTX
Microsoft PowerPoint Student PPT slides.pptx
Garleys Putin
 
PPTX
Artificial-Intelligence-in-Daily-Life (2).pptx
nidhigoswami335
 
PDF
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
PPTX
LESSON-2-Roles-of-ICT-in-Teaching-for-learning_123922 (1).pptx
renavieramopiquero
 
PDF
Data Protection & Resilience in Focus.pdf
AmyPoblete3
 
Cybersecurity Awareness Presentation ppt.
banodhaharshita
 
BGP Security Best Practices that Matter, presented at PHNOG 2025
APNIC
 
Pengenalan perangkat Jaringan komputer pada teknik jaringan komputer dan tele...
Prayudha3
 
The Internet of Things (IoT) refers to a vast network of interconnected devic...
chethana8182
 
LB# 820-1889_051-7370_C000.schematic.pdf
matheusalbuquerqueco3
 
谢尔丹学院毕业证购买|Sheridan文凭不见了怎么办谢尔丹学院成绩单
mookxk3
 
dns domain name system history work.pptx
MUHAMMADKAVISHSHABAN
 
Introduction to dns domain name syst.ppt
MUHAMMADKAVISHSHABAN
 
The Internet of Things (IoT) refers to a vast network of interconnected devic...
chethana8182
 
AI ad its imp i military life read it ag
ShwetaBharti31
 
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 
办理方法西班牙假毕业证蒙德拉贡大学成绩单MULetter文凭样本
xxxihn4u
 
Transformaciones de las funciones elementales.ppt
rirosel211
 
Latest Scam Shocking the USA in 2025.pdf
onlinescamreport4
 
Perkembangan Perangkat jaringan komputer dan telekomunikasi 3.pptx
Prayudha3
 
Microsoft PowerPoint Student PPT slides.pptx
Garleys Putin
 
Artificial-Intelligence-in-Daily-Life (2).pptx
nidhigoswami335
 
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
LESSON-2-Roles-of-ICT-in-Teaching-for-learning_123922 (1).pptx
renavieramopiquero
 
Data Protection & Resilience in Focus.pdf
AmyPoblete3
 

Create a Core Data Observer in 10mins

  • 1. Build your own Core Data Observer Zach McArtor, @zmcartor, http://hackazach.net
  • 2. Wat? • Small component that notifies us of NSManagedObject changes/saves • React to those changes via method invocation
  • 3. Why? • Sometimes, an NSFetchedResultsController won’t cut it • Want to update a tiny piece of UI or run a background action • Be reactive!
  • 4. Relevant Notifications Fired by NSManagedObjectContext • NSManagedObjectContextObjectsDidChangeNotification • NSManagedObjectContextDidSaveNotification
  • 5. Subscribe, and ye shall receive [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) name:NSManagedObjectContextDidSaveNotification object:SomeManagedObjectContext];
  • 6. An NSDict of Changes! • NSDict keyed by these constants: • { NSInsertedObjectsKey, • NSUpdatedObjectsKey • NSDeletedObjectsKey. }
  • 7. Last step, add a predicate • Subscribing to didChange/DidSave triggers a callback for every model save/change. We only care about a subset of models. • Abstract notification subscription into Class • Predicate filter models returned in NSDict. (Protip!)
  • 8. Dealing Concurrency With • There are different ways to set up a concurrent Core Data stack :( ! ! ! http://floriankugler.com/blog/2013/4/2/theconcurrentcoredatastack https://www.cocoanetics.com/2012/07/multi-context-coredata/
  • 9. Which context to subscribe? • ‘didSave’ notifications fired during context save (duh.) Going up chain child->parent • ’didChange’ notifications fired when context merges changes (MagicalRecord merges down into default context)
  • 10. Watch out! • The managedObjects received via notification may not be in the correct context for use on main thread. • Use existingObjectWithId:error: to pull into correct context • Subscribe to a single context, don’t listen for every event within a nested system
  • 11. Thanks for observing this talk! Reference implementation : https://github.com/mzarra/ZDS_Shared/blob/master/ ZSContextWatcher.m