-->

Cocoa/Core Data - Accessing App Delegate and manag

2019-06-09 02:03发布

问题:

I'm still very new to Cocoa programming and I'm having some issues understanding how/when certain objects are initialised when an application is launched, particularly with regard to Core Data. I'll explain what I currently have working and what I am looking to do:

Current Setup

I have an NSOutlineView that I'm using as a Source List. The NSOutlineView was created using the Interface Builder in the MainMenu.xib file. The dataSource and the delegate for the NSOutlineView is my AppDelegate file. The data that is displayed in my Source List are objects that have been stored via Core Data. At the moment all my code is in App Delegate. Everything is working fine with this setup, no issues.

What I'm Looking for:

What I would like to have is a separate file that is used as the dataSource and delegate for the NSOutlineView. So I'm pretty much wanting to take all the code related to my Source List out of AppDelegate and place it in my new file.

What I've done so far:

I've created my new file, LTSidebarViewController, that implements NSOutlineViewDelegate and NSOutlineViewDataSource. I have all the required methods in this file, - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item etc. I've also gone into MainMenu.xib and changed the NSOutlineView's dataSource and delegate to point to LTSidebarViewController. I have a method in LTSidebarViewController called managedObjectContext that returns the App Delegate's managedObjectContext like so:

-(NSManagedObjectContext *)managedObjectContext{
    return [[NSApp delegate] managedObjectContext];
}

So, with this all setup, inside the init method for LTSidebarViewController I'm trying to create an array that stores all of the objects currently stored in Core Data. This is where the problem is. I can't access App Delegate's managedObjectContext from LTSidebarViewController, it always returns null. I can't access anything in the App Delegate. The application does launch without any errors but obviously nothing is displayed in the Source List. Also, even though the app launches without any errors, the applicationDidFinishlaunching method doesn't appear to be getting called as none of the NSLog's I have in there are showing.

As a test to try to visualise what's happening when the app starts, I added an init method to the App Delegate and put in a few NSLog methods to output the managedObjectContext. I then did the same thing in the init method for LTSidebarViewController. The results are what is confusing me. This is what I get in the console:

2012-11-22 10:58:51.297 LecturesTest[70876:303] App Del init
2012-11-22 10:58:51.309 LecturesTest[70876:303] App Del Managed Obj: <NSManagedObjectContext: 0x10063f9a0>
2012-11-22 10:58:51.410 LecturesTest[70876:303] Controller init
2012-11-22 10:58:51.412 LecturesTest[70876:303] Managed Obj: (null)

From this my understanding is that the App Delegate is initialised, and the managedObjectContext is created. Then LTSidebarViewController is then initialised and tries to access the App Delegate's managedObjectContext via the method I mentioned above but for some reason is empty.

This is what I don't understand. How can the App Delegate be initialised and have its managedObjectContext created but not be accessible from another class afterwards?

Am I missing something or is my design just simply not very good? I would have thought it would be possible to access the App Delegate's managedObjectContext from other files.