How do I load PersistentDocuments into the same window

Posted by Brad Stone on Stack Overflow See other posts from Stack Overflow or by Brad Stone
Published on 2010-04-30T16:53:02Z Indexed on 2010/04/30 16:57 UTC
Read the original article Hit count: 224

Filed under:
|
|

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!

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about core-data