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 theDataController
. - I made the
DataController
a singleton. - I forwarded the true
AppDelegate
related methods to theDataController
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.)
- What magic glue code, that is hidden in the
CoreData
template makes cmd+s work and enables the File - Save menu item? - What hidden code restores the content of my
mangedObjectContext
to myOutlineView
on application launch.