In Xcode 6 (7 too), I'm looking to find out how to get managedObjectContext and use it in a ViewController in a different scene.
- I started a new Cocoa project, clicked 'Use CoreData'
- Added an entity in CoreData, generated a managedObject for it
- Added an array controller, and bound it to the entity
- I want to bind the array controller to the managedObjectContext, but I also want to bind the tableView (in my view controller) to the array controller. They are in different scenes, which seems to mean different namespaces.
What I've tried to do: Set an instance variable in ViewController:
var managedObjectContext: NSManagedObjectContext!
In viewDidAppear() I added:
if let app = NSApplication.sharedApplication().delegate! as? AppDelegate {
if let context = app.managedObjectContext{
managedObjectContext = context
} else {
print("There was no context available, didn't work")
}
}
Then I bound the columns of the table to the properties of the entity. And cocoa bindings autocompleted, meaning the context was at least recognized properly.
However when I run it, it fails silently with: 'Cannot perform operation without a managed object context'. When debugging the context is being set as a real object, but I have no idea if it's actually initialized. I looked through the docs and cocoa binding troubleshooting but this seems to be a coredata issue.
(I've looked here: Getting managedObjectContext from AppDelegate but I can't override the normal init in swift)

