2

If you are not interested in my story, jump to the two numbered questions on the bottom now.

In this Question, it is discussed whether or not to separate the CoreData handling from the AppDelegate. I decided to try the separation of concerns way.

Since Apple does not provide documentation on that topic for AppKit applications, my question is:

  • Is there any documentation, resource or even a sample project that shows how to separate the CoreData stack from the AppDelegate?

My current state is this:

I have an AppDelegate and a DataController which is a subclass of NSTreeController. The DataController controls a NSOutlineView which shows objects (groups and leafs) of my CoreData model.

I started with the CoreData (not Document based) template from Xcode.

  • I moved all CoreData-Stack related methods from the AppDelegate to the DataController.
  • I made the DataController a singleton.
  • I forwarded the true AppDelegate related methods to the DataController like so:

In AppController.m

- (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];
}
  • All those called methods were of course implemented in DataController.m

Now I am able to add and remove objects in the NSOutlineView, 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.)

When I quit the application my objects in the OutlineView are written to the persistentStore (I saw the xml) through the applicationShouldTerminate call. However, when I restart the application, the objects are not restored to the OutlineView. (This used to work 'magically' when I had the CoreData stack in AppDelegate.)

  1. What magic glue code, that is hidden in the CoreData template makes cmd+s work and enables the File - Save menu item?
  2. What hidden code restores the content of my mangedObjectContext to my OutlineView on application launch.
Community
  • 1
  • 1
Stefan
  • 91
  • 8
  • I've only done this for iOS so it's not exactly what you're looking for but the implementation I use is based on http://nachbaur.com/blog/smarter-core-data – Phillip Mills Aug 20 '12 at 21:48
  • You need to have the file save action in the responder chain, so you could either add a controller to the responder chain or consider implementing it in the app delegate - which is in the responder chain by default. – Monolo Aug 22 '12 at 20:36
  • @Monolo Thanks for mentioning `responder chain`! I connected (using right click) the `First Responder`s saveAction to the Save... menu item. Since I'm already forwarding the saveAction of the `AppDelegate` to the `DataController`, the menu item was enabled and cmd+s works too. – Stefan Aug 23 '12 at 16:58

1 Answers1

0

I've written a framework for iOS that does this. My approach takes the Core Data boilerplate code from the AppDelegate and moves it into a singleton class. This gives a clean separation from the AppDelegate, removes the need to pass a NSManagedObjectContext among view controllers, and lets you add Core Data into an application by just copying the framework into your project.

You can read about the framework at http://schwiiz.org/?p=1120 or download it from https://github.com/chriscdn/RHManagedObject.

The framework is more than just a singleton. It also extends NSManagedObject to simplify things like saving among different threads and fetching.

chris
  • 16,324
  • 9
  • 37
  • 40
  • Thanks for your answer and the link. I had a look at your project and it looks very solid. However, I guess the issues I have are really platform dependent. – Stefan Aug 22 '12 at 20:32