views:

17

answers:

2

I want to open NSPersistentDocuments and load them into the same window one at a time. I'm almost there but missing some steps. Hopefully someone can help me.

I have a few saved documents on the hard drive. On launch my app opens to an untitled NSPersistentDocument and creates a separate NSWindowController. When I press the button to load file 1 off the hard drive the data appears in the fields but two things are wrong that I can see:

1) changing the data doesn't make the document dirty
2) choosing save updates the persistentstore (I know this because when I open the file again I see the changes) but I get an error: +entityForName: could not locate an NSManagedObjectModel for entity name 'Book'

Here's my code which is in the WindowController that was launched initially with the untitled document. This code isn't perfect. For example, I know I should processPendingChanges and save the current doc before I load the new one. This is test code to try to get over this hurdle.

- (IBAction)newBookTwo:(id)sender {
 NSDocumentController *dc = [NSDocumentController sharedDocumentController];
 NSURL *url = [NSURL fileURLWithPath:[@"~/Desktop/File 2.binary" stringByExpandingTildeInPath]];

 NSError *error;
 MainWindowDocument *thisDoc = [dc openDocumentWithContentsOfURL:url display:NO error:&error];

 [self setDocument:thisDoc]; 
 [self setManagedObjectContext:[thisDoc managedObjectContext]];
}

Thanks!

A: 

Assuming you have an entity called "Book" or "book" then the second problem is most likely a typo swapping case. Depending on the entity graph that may be triggering your first problem as well.

TechZen
The error comes up after the doc is saved. I know this because when I repoen it the changes are in there.
Brad Stone
Actually, it probably comes up AS the doc is saved. Core Data is biased to save data. It will save everything it can even if it hits an error. In any case, the second error is definitely in the entity graph or in some code that calls the entity graph.
TechZen
A: 

Put a break point in there and see if your model is nil.

Marcus S. Zarra