Andy
Andy

Reputation: 335

Core Data NSArrayController Doesn't Save or Undo Programmatic Elements

I have a document-based Cocoa application in OS X with a Core Data backed NSArrayController bound to the columns of an NSTableView and "Add" and "Remove" buttons.

This all works fine, objects added and modified using these bindings get added to the undo stack and save to files as expected.

However when I .addObject() programmatically, it is reflected in the table (and therefore, it would seem, the NSManagedObjectContext), but not added to the undo stack, or saved to files.

What am I missing? Some setting in my NSArrayController? Or some other call after .addObject()?

I have heard "Handles Content As Compound Value" mentioned in relation to similar problems, but this seems to be when using the Content Array binding, which I am not.

Upvotes: 1

Views: 228

Answers (1)

Hal Mueller
Hal Mueller

Reputation: 7646

Are you calling .addObject() on the array that your NSArrayController is managing? That won't work because you are bypassing Core Data.

To insert programmatically, use insertNewObjectForEntityForName(inManagedObjectContext). Then the array controller will see the change. Or call add() on your array controller, which is what your Add button is doing. But insertNewObjectForEntityForName() is a better choice, because it will work independent of whether you have an active NSDocument.

Upvotes: 1

Related Questions