mogenerator, or how I nearly abandoned Core Data

December 19, 2006 at 6:24 pm 4 comments

Unfortunately, my Macbook Pro is out of commission due to a broken fan (I’ve sent it in to Apple; they’d better send it back soon! I’m dying here!), so I’m just going to blog about a mini-renaissance I had when working with Core Data last week.

When I first saw Core Data, I couldn’t believe my eyes. Apple’s developer tools now had a simple way to graphically model as many parts of an MVC application as possible – Interface Builder for the view, Cocoa Bindings (and custom logic) for the controller, and now Core Data for the modeling. Add in the fact that I had heard nothing but praise for it, and I was completely sold. I created the model for my application, prototyped a view, and flipped to the documentation on Core Data’s NSManagedObject.

It was, to say the least, an unpleasant surprise. Though I adored the fact that Core Data would take care of undo/redo, saving, archival formats, and saving data, I didn’t want to start using valueForKey:. setValue: forKey, primitiveValueForKey:, and setPrimitiveValue: forKey: instead of the mutator methods that I had grown to love for their combination of ease-of-use and added maintainability. Sure, I could have made a million subclasses of NSManagedObject, but the idea of doing that manually struck me as tedious – and if there’s anything I loathe, it’s tedium. And updating every object every time I made a change to the Core Data model struck me as a maintainability nightmare.

Though it may reflect poorly on me as a programmer, I considered abandoning Core Data. The magic which it brought to undo/redo/saving/archiving could not overcome the reluctance I had to view all my objects as NSManagedObjects – which, frankly, seems like quite a breach of the Model part of the MVC philosophy.

But hope lay in wait. At the bottom of some Google results about subclassing NSManagedObject, I found this page from Jonathan ‘Wolf’ Rentzsch – one of my idols, both for his coding and his vocal pro-Cocoa advocacy – about an unbelievably clever tool named mogenerator.

In short, mogenerator reads the data you have stored in an .xcdatamodel file, extracts the information about each Entity one has created, creates two subclasses of NSManagedObject for each Entity, changes the .xcdatamodel file automatically so that your Entities inherit from their proper classes, and adds code for all necessary accessor and mutator methods – in short, it removes everything I resented about Core Data.

Why two subclasses? Because if I decide to make a change to the data model, I don’t wont to worry about overwriting my own code with the automatically generated code that mogenerator creates for me. To solve this problem, Wolf has his program use one of the two files for automatically generated code, and allows one to use the second – which extends the first file’s class – for one’s own nefarious purposes. If you ever update the .xcdatamodel file, all you need to do is run mogenerator again, and only the file with the automatically generated code will be overwritten; you can be sure that the custom application logic you’ve written will stay intact.

This is a stunningly useful program, and I don’t know why people aren’t proclaiming it’s merits hither and yon. mogenerator allows one to forgo all compromise with Core Data – you get all the advantages of an NSManagedObject without sacrificing the familiar paradigm of creating specific .c/.h files for each object’s code. My thanks go out to Rentzsch for such an amazing tool.

Entry filed under: cocoa, code, coredata, mvc, objc, problems.

It’s Hard Out Here for an MVC Advocate I got you a present!


  • 1. Scott Stevenson  |  December 24, 2006 at 9:37 pm

    A few things:

    1. You’d probably like TextMate, as it largely negates the issue of writing boilerplate code

    2. Xcode will generate accessors for you if you right-click on various things in the model editor

    3. You will love Objective-C 2.0

  • 2. Patrick  |  December 24, 2006 at 9:43 pm

    (Oh my God, Scott Stevenson is leaving comments on my blog)

    1. I do indeed love Textmate. I could not live without it.
    2. Neat! Didn’t know that.
    3. I am so excited for ObjC 2.0 – and Leopard in general.

  • 3. Scott Stevenson  |  December 24, 2006 at 9:59 pm

    At some point the ‘cdacc’ tab trigger appeared in TextMate, which is pretty helpful. I don’t know if you’re aware of it but I missed it for a while.

  • 4. rentzsch  |  December 25, 2006 at 7:58 am


    Glad you’re enjoying mogenerator! It’s stories like these that make me happy I put in the effort to make it public.

    One clarification: mogenerator never modifies your .xcdatamodel file. It would be kind of cool if it could, but it’s Apple’s undocumented format and I haven’t had much reason to reverse engineer it yet.


    I get the impression you think mogenerator is mostly about boilerplate code. I can see why, since I frame it in terms of code generation.

    Truth is, mogenerator is more about code structure than code gen. Sure tools like Accessorizer and TextMate can ease the chore of writing template code, but a key feature of mogenerator is the machine generated code stays in sync with your model with minimal effort (ideally, no effort, but sadly that’s not the case yet). With TextMate, it’s on your shoulders to remember to go back and regen your wrappers based on your model modifications.

    It’s not a huge deal, but personally I’d often forget to go back or I’d screw up the modification. Since the machine can do it for me, why not let it?

    The ObjC 2.0 story is interesting. I actually see mogenerator becoming more valuable in the face of the upcoming enhancements.

    But that could just be the delusion of a proud papa.

About Me

I'm Patrick Thomson. This was a blog about computer programming and computer science that I wrote in high school and college. I have since disavowed many of the views expressed on this site, but I'm keeping it around out of fondness.

If you like this, you might want to check out my Twitter or Tumblr, both of which are occasionally about code.

Blog Stats

  • 695,966 hits

%d bloggers like this: