Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
41 lines (26 sloc) 1.59 KB


There is a lot of boilerplate code required to write a Core Data application. This is annoying. In pretty much everything I've written since Core Data came to iOS, I have used the following class.

What's Included


  • Manages main context, persistent store, etc
  • Accessing entity descriptions
  • Reflection
  • Easy creating and deleting


  • Observe inserted and updated objects in a context


  • Adds hooks for SSManagedObjectContextObserver

Using the Context Observer

You can use SSManagedObjectContextObserver to observe when any object is inserted or updated in a context. You can optionally filter this by setting its entity property.

Example: (Photo is a subclass of SSManagedObject)

SSManagedObjectContextObserver *observer = [[SSManagedObjectContextObserver alloc] init];
observer.entity = [Photo entityDescription];
observer.observationBlock = ^(NSSet *insertedObjects, NSSet *updatedObjects) {
	NSLog(@"Inserted %i photos. Updated %i photos.", insertedObjects.count, updatedObjects.count);
[[SSManagedObject mainContext] addObjectObserver:observer];
[observer release];

The observationBlock will be handed sets of NSManagedObjectIDs for the inserted and updated objects after a save completes on the observed context. This is handy if you need to do background processing on large sets of changing objects.

If you need to observe deletes, modify an object before save, get objects ordering, etc, you should use the hooks in NSManagedObject or use NSFetchedResultsController.