Core Data is not Active Record

09 Aug 2013

Core Data is a mature framework that has been around since 10.4 - even Apple uses it. It’s built for fast native apps. There are damn good reasons Apple exposed its API like this.

Core Data works with:

  • NSPersistentStores - these store your data
  • NSPersistentStoreCoordinators - these coordinate persistence between the context and the store
  • NSManagedObjectContexts - these are scratch pads for your object graph
  • NSManagedObjects - these are managed by the core data stack you implement

A NSManagedObject is bound to a NSManagedObjectContext, it’s why the initalizer requires a managed object context.

That’s why we use it like this:

Person *bob = [[Person alloc] initWithEntity:entity insertIntoManagedObjectContext:context];

and not this:

Person *bob = [Person create];

Active Record doesn’t have persistent store coordinators, and its objects aren’t bound to managed object contexts, that are not thread safe .

Using a wrapper to abstract these concepts away from Core Data can lead to anti patterns and confuse developers. Adding a styntactical layer doens’t make it work any differetly. These attempts to ‘elimante boiler plate’ mask the complexity of Core Data at best.

I’m not advocating that MagicalRecord and friends are bad, but use with care. Why discard a robust architecture and proven design pattern in an attempt to make Core Data look like something its clearly not?

Published on 09 Aug 2013 Find me on Twitter!