<?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[NSManagedObject: better than sliced bread?&nbsp;(Yes.)]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>(Update: as usual, <a href="http://www.theocacao.com/">Scott Stevenson</a> totally kicked my ass:<br />
<code><br />
+ (EPFoobarMO *)foobarWithDefaultContext<br />
{<br />
return [NSEntityDescription insertNewObjectForEntityForName:@"Foobar" inManagedObjectContext: [[NSApp delegate] managedObjectContext];<br />
}<br />
</code><br />
I often forget that I&#8217;m a cretin preaching to a crowd of really smart people.)</p>
<p>I kid you not: NSManagedObject and Core Data are becoming some of my favorite coding structures/paradigms ever. (I suppose I have Wolf Rentzsch to thank; by the way, <a href="http://www.rentzsch.com/code/mogenerator_v1.1">mogenerator</a> v1.1 is out. Check it out.) It&#8217;s just so awesome &#8211; so nice not to have to futz around with NSUndoManager, NSCoding, and all the other minutiae that some brave souls worked with in years past.</p>
<p>I got rejected &#8211; not just deferred, rejected &#8211; from the University of Chicago. 😦 As such, I don&#8217;t really have time to write a very lengthy entry; therefore, I&#8217;ll just leave you with this little question/guide.</p>
<p>The official way of instantiating an instance/subclass of NSManagedObject is (I think) to use the NSManagedObject class method<code>initWithEntity:insertIntoManagedObjectContext:</code>, like so: (we are assuming that there&#8217;s an NSManagedObjectModel and NSManagedObjectContext named <code>model</code> and <code>context</code>, respectively:<br />
<code><br />
NSEntityDescription *e = [[model entitiesByName] objectForKey: @"Foobar"];<br />
EPFoobarMO *foo = [[EPFoobarMO alloc] initWithEntity: e insertIntoManagedObjectContext: context];<br />
</code></p>
<p>Since they&#8217;re nice people, Apple gave us a convenience method:<br />
<code><br />
EPFoobarMO *foo = [NSEntityDescription insertNewObjectForEntityForName: @"Foobar" inManagedObjectContext: context];<br />
</code></p>
<p>But if we&#8217;re sticking to the MVC paradigm (which we are, <a href="https://importantshock.wordpress.com/2006/12/06/its-hard-out-here-for-an-mvc-advocate/">aren&#8217;t we?</a>), one wants to put the initialization code for the EPFoobarMO object inside the EPFoobarMO .m/.h file.<br />
<code><br />
+ (EPFoobarMO *)foobarWithContext:(NSManagedObjectContext *)context<br />
{<br />
// According to the docs the object returned is autoreleased<br />
return [NSEntityDescription insertNewObjectForEntityForName:@"Foobar"<br />
inManagedObjectContext:context];<br />
}<br />
</code><br />
But it&#8217;s ugly to have to pass the NSManagedObjectContext to the initializer every time. We could stick it inside the app&#8217;s controller:<br />
<code><br />
- (EPFoobarMO *) foobar<br />
{<br />
return insertNewObjectForEntityForName:@"Foobar"<br />
inManagedObjectContext:[self managedObjectContext]];<br />
}<br />
</code><br />
But that&#8217;s a flagrant violation of the MVC rules. (Of course, if you&#8217;re ignoring them, then this is an ideal solution. But down that way lies Nyarlathotep.)</p>
<p>Or we could resort to the insanity that is the C preprocessor. (I would give a code sample, but I don&#8217;t want to hate myself.)</p>
<p>Anyway, what do you think? Leave comments with your opinions or to alert me of any blatant inaccuracies.</p>
]]></html></oembed>