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 actio...
Relevant Notifications
Fired by NSManagedObjectContext
• NSManagedObjectContextObjectsDidChangeNotification
• NSManagedObjec...
Subscribe, and ye shall receive
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contextDidSave:)...
An NSDict of Changes!
• NSDict keyed by these constants:
• { NSInsertedObjectsKey,
• NSUpdatedObjectsKey
• NSDeletedObject...
Last step, add a predicate
• Subscribing to didChange/DidSave triggers a
callback for every model save/change. We only
car...
Dealing Concurrency With
• There are different ways to set up a concurrent
Core Data stack :(
!
!
!
http://floriankugler.co...
Which context to subscribe?
• ‘didSave’ notifications fired during context save
(duh.) Going up chain child->parent
• ’didCh...
Watch out!
• The managedObjects received via notification
may not be in the correct context for use on
main thread.
• Use e...
Thanks for
observing this
talk!
Reference implementation :
https://github.com/mzarra/ZDS_Shared/blob/master/
ZSContextWatc...
Upcoming SlideShare
Loading in …5
×

Create a Core Data Observer in 10mins

3,657 views

Published on

Create a small component to observe changes in Core Data and notify you when interesting things change.

Published in: Internet, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,657
On SlideShare
0
From Embeds
0
Number of Embeds
1,565
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Create a Core Data Observer in 10mins

  1. 1. Build your own Core Data Observer Zach McArtor, @zmcartor, http://hackazach.net
  2. 2. Wat? • Small component that notifies us of NSManagedObject changes/saves • React to those changes via method invocation
  3. 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. 4. Relevant Notifications Fired by NSManagedObjectContext • NSManagedObjectContextObjectsDidChangeNotification • NSManagedObjectContextDidSaveNotification
  5. 5. Subscribe, and ye shall receive [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) name:NSManagedObjectContextDidSaveNotification object:SomeManagedObjectContext];
  6. 6. An NSDict of Changes! • NSDict keyed by these constants: • { NSInsertedObjectsKey, • NSUpdatedObjectsKey • NSDeletedObjectsKey. }
  7. 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. 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. 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. 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. 11. Thanks for observing this talk! Reference implementation : https://github.com/mzarra/ZDS_Shared/blob/master/ ZSContextWatcher.m

×