0
votes

What's the best way to use Core Data if each document on disk corresponds to one Entity instance?

I have a data model file with one entity, and that entity has one attribute of name text and of type Text.

I have a Document.xib that has an NSObjectController that is set to 'Entity' mode and gets the managedObjectContext from the File's Owner. I have an NSTextField that is bound to the Object Controller for the Controller Key 'selection' and the Key Path 'text.' (This is just a test so I can figure out how Core Data works, but my eventual app will also only have one Entity instance per Document)

When I create a new document the textfield says 'No Selection' and is disabled.

I imagine that if I had a Table View or some other kind of way to select from among entity instances the selection would work but I don't nor do I want to. How can I hook up the NSObjectController to only have one Entity instance and to automatically 'select' it?

The intended behaviour is that I type something into the NSTextField, hit Save, close the document, re-open the document and the string in the textfield persists.

This is probably a really basic question but I can't find any tutorials or documents that would address this seemingly simple use case.

1

1 Answers

0
votes

Ok, well I haven't figured it all out but my particular issue was being caused by the fact that nothing was being created. I switched out the NSObjectController for an NSArrayController, created an outlet for it in Document.m and added this to windowControllerDidLoadNib:

if (![self.arrayController selectedObjects]) {
    [self.arrayController add:@""];
};

Now it seems to just manage the one Entity object.