<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[Important Shock]]></provider_name><provider_url><![CDATA[https://importantshock.wordpress.com]]></provider_url><author_name><![CDATA[Patrick Thomson]]></author_name><author_url><![CDATA[https://importantshock.wordpress.com/author/importantshock/]]></author_url><title><![CDATA[mogenerator, or how I nearly abandoned Core&nbsp;Data]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>Unfortunately, my Macbook Pro is out of commission due to a broken fan (I&#8217;ve sent it in to Apple; they&#8217;d better send it back soon! I&#8217;m dying here!), so I&#8217;m just going to blog about a mini-renaissance I had when working with Core Data last week.</p>
<p>When I first saw Core Data, I couldn&#8217;t believe my eyes. Apple&#8217;s developer tools now had a simple way to graphically model as many parts of an MVC application as possible &#8211; 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&#8217;s <code>NSManagedObject</code>.</p>
<p>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&#8217;t want to start using <code>valueForKey:</code>. <code>setValue: forKey</code>, <code>primitiveValueForKey:</code>, and <code>setPrimitiveValue: forKey:</code> 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 <code>NSManagedObject</code>, but the idea of doing that manually struck me as tedious &#8211; and if there&#8217;s anything I loathe, it&#8217;s tedium. And updating every object every time I made a change to the Core Data model struck me as a maintainability nightmare.</p>
<p>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 &#8211; which, frankly, seems like quite a breach of the Model part of the MVC philosophy.</p>
<p>But hope lay in wait. At the bottom of some Google results about subclassing NSManagedObject, I found <a href="http://rentzsch.com/code/mogenerator">this page</a> from <a href="http://rentzsch.com/">Jonathan &#8216;Wolf&#8217; Rentzsch</a> &#8211; one of my idols, both for his coding and his vocal pro-Cocoa advocacy &#8211; about an unbelievably clever tool named <strong>mogenerator</strong>.</p>
<p>In short, mogenerator reads the data you have stored in an .xcdatamodel file, extracts the information about each Entity one has created, creates <em>two</em> 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 &#8211; in short, it removes everything I resented about Core Data.</p>
<p>Why two subclasses? Because if I decide to make a change to the data model, I don&#8217;t wont to worry about overwriting my own code with the automatically generated code that <strong>mogenerator</strong> 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 &#8211; which extends the first file&#8217;s class &#8211; for one&#8217;s own nefarious purposes. If you ever update the .xcdatamodel file, all you need to do is run <strong>mogenerator</strong> again, and only the file with the automatically generated code will be overwritten; you can be sure that the custom application logic you&#8217;ve written will stay intact.</p>
<p>This is a stunningly useful program, and I don&#8217;t know why people aren&#8217;t proclaiming it&#8217;s merits hither and yon. <a href="http://rentzsch.com/code/mogenerator">mogenerator</a> allows one to forgo all compromise with Core Data &#8211; you get all the advantages of an NSManagedObject without sacrificing the familiar paradigm of creating specific .c/.h files for each object&#8217;s code. My thanks go out to Rentzsch for such an amazing tool.</p>
]]></html></oembed>