Digital Flapjack

The Digital Flapjack Blog Contents RSS

Making CoreData slicker with mogenerator

Posted by Michael Dales on 2010-03-26 12:16:42

After returning to Cocoa development after a long absence, it was great to discover all the new frameworks there are now to help you get your apps together quickly. After getting used to ORMs as a way of abstracting away databases whilst doing web development, I was very pleased to get to grips with CoreData, Cocoa's abstraction over the sqlite database.

CoreData has lots of nice features. Despite being a coder, I like the way I design with my data model in the model editor outside of code, then work with managed objects in my code - it's a boundary that seems to work well. XCode helps make this relationship work by auto generating (on request) code for custom managed object subclasses for each table in your data model.

Unfortunately, these code files are hard to maintain, due to the usual dilemma of auto generated code. If you change your model you're faced with two choices: update the files yourself, laborious but does mean you can add logic code to these subclasses without fear of losing it, or you can have XCode regenerate them to reflect the changes, but you can't really keep any logic code in those files. Another slight annoyance is that these subclasses only really save you from using key/value accessing managed objects - they don't save you from a lot of boiler plate code that you end up writing with CoreData to do with queries and creating new objects.

But there is a solution, thanks to Jonathan 'Wolf' Rentzsch, called mogenerator. mogenerator is like XCode's subclass generator, but on steroids. Firstly, it generates two classes per table. The first is a direct subclass of NSManagedObject and adds accessor methods for attributes and relationships specific for this table, just like XCode. However, it also generates a second subclass, which is a subclass of the first one, which is the one you use and can edit freely, as mogenerator will only ever update the first subclasses's files after creation. Thus you can add your own table specific login to this file without ever worrying it'll be lost when mogenerator is run again after you've updated your CoreData model.

The second very useful thing mogenerator does is it creates more than just direct accessor methods, it adds a bunch of useful calls that help reduce the amount of boilerplate code you need to write. For example, there's a method for creating a new managed object instance without having to also remember the CoreData table name - not having this is clearly an opportunity for mistakes to creep in (and I've just submitted a patch that will also do the same for making entity descriptions this way). It also adds hugely useful value accessors for numeric values, so in addition to getting the NSNumber object, you can also get and set using integers or floats as appropriate - another useful time saver.

You could do most of what mogenerator does yourself, but why should you? mogenerator lets you just get on with coding. If all this wasn't enough, if you install mogenerator it comes with a tool that, if you set a comment "xmod" on your CoreData model (under GetInfo in XCode) then mogenerator will automatically be ran any time you save changes to the model, so your code is always up to date in reference to the model. It really is a must if you're playing with CoreData.