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
PDF
High Performance Core Data
PDF
Adventures in Multithreaded Core Data
KEY
iOSDevCamp 2011 Core Data
PDF
Multi-threaded CoreData Done Right
PPT
Core data orlando i os dev group
PDF
Modern Android app library stack
ODP
Indexed db
Core Data with multiple managed object contexts
High Performance Core Data
Adventures in Multithreaded Core Data
iOSDevCamp 2011 Core Data
Multi-threaded CoreData Done Right
Core data orlando i os dev group
Modern Android app library stack
Indexed db

What's hot (20)

PDF
Simpler Core Data with RubyMotion
TXT
Birhanu distributive assignment
PPTX
Core Data Performance Guide Line
PPTX
SenchaCon 2016: The Once and Future Grid - Nige White
PDF
ERGroupware
PPT
Core Data Migration
PDF
iOS for ERREST - alternative version
PPTX
Whats New for WPF in .NET 4.5
PPTX
Angular JS deep dive
PDF
[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules
ZIP
Easy undo.key
PDF
Александр Терещук - Memory Analyzer Tool and memory optimization tips in Android
PDF
Realm.io par Clement Sauvage
ODP
Polyglot persistence with Spring Data
PPTX
Creating Single Page Web App using Backbone JS
PPTX
Redux + RxJS + Ember makes simple
PDF
MongoDB Performance Tuning
PPTX
Dbabstraction
PPT
Zend framework 03 - singleton factory data mapper caching logging
PPTX
Google apps script database abstraction exposed version
Simpler Core Data with RubyMotion
Birhanu distributive assignment
Core Data Performance Guide Line
SenchaCon 2016: The Once and Future Grid - Nige White
ERGroupware
Core Data Migration
iOS for ERREST - alternative version
Whats New for WPF in .NET 4.5
Angular JS deep dive
[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules
Easy undo.key
Александр Терещук - Memory Analyzer Tool and memory optimization tips in Android
Realm.io par Clement Sauvage
Polyglot persistence with Spring Data
Creating Single Page Web App using Backbone JS
Redux + RxJS + Ember makes simple
MongoDB Performance Tuning
Dbabstraction
Zend framework 03 - singleton factory data mapper caching logging
Google apps script database abstraction exposed version
Ad

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

ODP
MobileCity:Core Data
PDF
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
PDF
Intro to Core Data
PPTX
Change tracking
PDF
Elements for an iOS Backend
PPTX
SenchaCon 2016: Ext JS + React: A Match Made in UX Heaven - Mark Brocato
PDF
Advanced Cassandra
PPT
Connecting to a REST API in iOS
PPTX
CQRS and Event Sourcing
PDF
An introduction to React.js
PPTX
React JS .NET
PDF
Core Data with Swift 3.0
PDF
PDF
vBrownBag 2015-07-08 - VCP6-DCV Foundations Section 7: Monitoring
PPTX
Using the Tooling API to Generate Apex SOAP Web Service Clients
PPTX
Managing state in asp.net
PDF
Using redux and angular 2 with meteor
PDF
Using redux and angular 2 with meteor
PDF
VMworld 2013: Extreme Performance Series: vCenter of the Universe
KEY
DjangoCon 2010 Scaling Disqus
MobileCity:Core Data
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
Intro to Core Data
Change tracking
Elements for an iOS Backend
SenchaCon 2016: Ext JS + React: A Match Made in UX Heaven - Mark Brocato
Advanced Cassandra
Connecting to a REST API in iOS
CQRS and Event Sourcing
An introduction to React.js
React JS .NET
Core Data with Swift 3.0
vBrownBag 2015-07-08 - VCP6-DCV Foundations Section 7: Monitoring
Using the Tooling API to Generate Apex SOAP Web Service Clients
Managing state in asp.net
Using redux and angular 2 with meteor
Using redux and angular 2 with meteor
VMworld 2013: Extreme Performance Series: vCenter of the Universe
DjangoCon 2010 Scaling Disqus
Ad

Recently uploaded (20)

PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PPTX
artificial intelligence overview of it and more
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PPTX
presentation_pfe-universite-molay-seltan.pptx
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PPTX
Introduction to Information and Communication Technology
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
Testing WebRTC applications at scale.pdf
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
Internet___Basics___Styled_ presentation
DOCX
Unit-3 cyber security network security of internet system
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
Introuction about ICD -10 and ICD-11 PPT.pptx
Slides PPTX World Game (s) Eco Economic Epochs.pptx
Decoding a Decade: 10 Years of Applied CTI Discipline
artificial intelligence overview of it and more
Tenda Login Guide: Access Your Router in 5 Easy Steps
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
presentation_pfe-universite-molay-seltan.pptx
WebRTC in SignalWire - troubleshooting media negotiation
Introduction to Information and Communication Technology
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Introuction about WHO-FIC in ICD-10.pptx
international classification of diseases ICD-10 review PPT.pptx
PptxGenJS_Demo_Chart_20250317130215833.pptx
introduction about ICD -10 & ICD-11 ppt.pptx
Testing WebRTC applications at scale.pdf
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Internet___Basics___Styled_ presentation
Unit-3 cyber security network security of internet system
Unit-1 introduction to cyber security discuss about how to secure a system

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