Cragly
Cragly

Reputation: 3626

Cocoa Cannot perform operation without a managed object context

I have a Mac window based application using CoreData and Cocoa bindings to bind a CoreData entity to an NSArrayController for displaying in a NSCollectionView.

I have a property on my controller (which is set as the views Files Owner) called managedObjectConext. In the awakeFromNib method I have tried setting the managedObjectContext (MOC) property to:

managedObjectContext = [(MyApplicationAppDelegate *)[[NSApplication sharedApplication] delegate] managedObjectContext];
managedObjectContext = [[NSApp delegate] managedObjectContext];

I have also configured the NSArrayControllers MOC bindings to the 'Files Owner' MOC property and attribute bindings to that of my CoreData entity.

However everytime I run the application just before its about to do the binding I get the follwowing error message:

Cocoa Cannot perform operation without a managed object context

I have logged in the console to check that the MOC is not nil, which I presume it isn't as this is the NSLog from the MOC object: myMoc = NSManagedObjectContext: 0x10052f9c0

I am totally stumped now and frustrated that something that should be so simple has taken up so much of my time! Any help would be greatly appreciated.

Upvotes: 1

Views: 3570

Answers (4)

Christian Krueger
Christian Krueger

Reputation: 139

With Swift 4.x you can create a var in ViewController like so:

   @objc dynamic var managedObjectContext:NSManagedObjectContext? = {
        let delegater = NSApplication.shared.delegate as! AppDelegate
        return delegater.persistentContainer.viewContext
}()

Then bind it to your ArrayController : enter image description here

Upvotes: 0

leerie simpson
leerie simpson

Reputation: 186

In swift, add this to whatever view controller the ArrayController is attached to:

    var managedObjectContext:NSManagedObjectContext? = {
            let delegater = NSApplication.sharedApplication().delegate as! AppDelegate
    return delegater.managedObjectContext
}()

Upvotes: 1

Matthias
Matthias

Reputation: 1162

I had the same problem. This fixed it:

Move the lines:

managedObjectContext = [(MyApplicationAppDelegate *)[[NSApplication sharedApplication] delegate] managedObjectContext];
managedObjectContext = [[NSApp delegate] managedObjectContext];

from the awakeFromNib method into the init method.

Upvotes: 3

TechZen
TechZen

Reputation: 64428

Bindings are defined in the nib, so if the nib doesn't know about the managedObjectContext property of the controller, then the bindings don't either.

In the interface editor, you need to bind the managedObjectContext key in the array controller's bindings panel to the specific context.

The easiest way to do this for a one context app, is to label the managedObjectContext property in the app delegate as an IBOutlet so that it shows up in the interface editor. Then just bind it directly from the array controller's panel.

Honestly, I don't know why they don't do that in the Xcode template itself. You end up doing it so often.

Upvotes: 5

Related Questions