title: How to separate CoreData management from the AppDelegate in OSX applications
body: <p>If you are not interested in my story, jump to the two numbered questions on the bottom now.</p>
  
  <p>In <a href="https://stackoverflow.com/questions/1267520/where-to-place-the-core-data-stack-in-a-cocoa-cocoa-touch-application">this Question</a>, it is discussed whether or not to separate the CoreData handling from the AppDelegate. I decided to try the separation of concerns way.</p>
  
  <p>Since Apple does not provide documentation on that topic for AppKit applications, my question is: </p>
  
  <ul>
  <li>Is there any documentation, resource or even a sample project that shows how to separate the CoreData stack from the AppDelegate?</li>
  </ul>
  
  <p>My current state is this:</p>
  
  <p>I have an <code>AppDelegate</code> and a <code>DataController</code> which is a subclass of <code>NSTreeController</code>. The <code>DataController</code> controls a <code>NSOutlineView</code> which shows objects (groups and leafs) of my <code>CoreData</code> model.</p>
  
  <p>I started with the <code>CoreData</code> (not Document based) template from Xcode. </p>
  
  <ul>
  <li>I moved all CoreData-Stack related methods from the <code>AppDelegate</code> to the <code>DataController</code>.</li>
  <li>I made the <code>DataController</code> a singleton. </li>
  <li>I forwarded the true <code>AppDelegate</code> related methods to the <code>DataController</code> like so:</li>
  </ul>
  
  <p>In AppController.m
  </p>
  
  <pre><code>- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window{
      return [[[DataController sharedController] managedObjectContext] undoManager];
  }
  - (IBAction)saveAction:(id)sender{
      [[DataController sharedController] saveAction:sender];
  }
  - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender{
      return [[DataController sharedController] applicationShouldTerminate:sender];
  }
  </code></pre>
  
  <ul>
  <li>All those called methods were of course implemented in DataController.m</li>
  </ul>
  
  <p>Now I am able to add and remove objects in the <code>NSOutlineView</code>, undo and redo also works. However, the File-Save menu item is grayed out, when I hit cmd+s I get the the bing from the OS. (This used to work 'magically' when I had the CoreData stack in AppDelegate.) </p>
  
  <p>When I quit the application my objects in the <code>OutlineView</code> are written to the <code>persistentStore</code> (I saw the xml) through the <code>applicationShouldTerminate</code> call. However, when I restart the application, the objects are not restored to the <code>OutlineView</code>. (This used to work 'magically' when I had the CoreData stack in AppDelegate.)</p>
  
  <ol>
  <li>What magic glue code, that is hidden in the <code>CoreData</code> template makes cmd+s work and enables the File - Save menu item?</li>
  <li>What hidden code restores the content of my <code>mangedObjectContext</code> to my <code>OutlineView</code> on application launch.</li>
  </ol>