Creating NSManagedObject Subclasses – So Handy, Yet So Strange

If you are a Core Data user, you’ll likely become intimately familiar with NSManagedObject and the Core Data model editor.

One aspect of NSManagedObject is the the creation of NSManagedObject subclasses. The only way I have found to (automatically) do this is to select your entities, then go to Editor -> Create NSManagedObjectSubclass.

Why on earth is ‘Create NSManagedObject Subclass’ a global menu┬áitem? And why is it in the ‘Editor’ menu? Whose brilliant idea was it to have a global menu item for something so incredibly specific to a particular portion of a particular screen? Why is generating code from your Core Data model so completely non-discoverable? Why isn’t there a ‘Create NSManagedObject Subclass’ button right next to the ‘Add Entity’ button?

And those are just my issues with the placement of the functionality. So aside from that, how well does it actually work?

In my limited usage so far, I would say that it’s marginally useable.

First of all, having to perform a manual process in order to keep your data model and your object model in sync is pointless. By contrast, when you edit an Entity Framework EDMX file or a LINQ to SQL DBML file, your object model classes are automatically regenerated. And hey, if you’re using EF Code-First, there’s nothing else you need to do!

Second, the code generation itself seems… inflexible. As far as I can tell, there is absolutely nothing remotely similar to .TT files in Xcode/Core Data. For those unfamiliar with the Visual Studio world, a .TT file is essentially a template that can describe, among other things, how classes are generated from a data model. You can customize a .TT file so that the generated classes look exactly like you want them to – add in validation attributes, extra fields, property change notifications, and so on. This standard functionality in EF/VS, but doesn’t seem to exist in Xcode.

In order to something similar, it appears that you need a third-party tool such as mogenerator. Now it’s great that such a tool exists, but it’s sad that it needs to exist. Customization of generated code is a pretty basic IDE feature these days, and it’s yet another area where Xcode falls short of the competition.

On another code generation note, it’s quite amusing that Xcode can’t even remember what group I previously added the NSManagedObject subclasses to, forcing me to manually select it every time I change my data model.

Leave a Reply

Your email address will not be published. Required fields are marked *