Loading PersistentDocuments into the same window
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!___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Loading PersistentDocuments into the same window
On Apr 30, 2010, at 08:56, Brad Stone wrote: I want to open NSPersistentDocuments and load them into the same window one at a time. Excuse the soapbox, but you do realize that from the outside looking in, this *seems* like a terrible idea? If your documents really are documents in the user interface sense, then what's the reason for inventing a new document-handling metaphor for users? If they're not really documents, then why try to shoehorn them into the NSDocument metaphor? Keep in mind that Core Data persistent stores don't really fit the standard document metaphor very well, so that NSPersistentDocument is already a little bit broken. (Ask the people who've had trouble with Save As, for example.) Piling on another layer of metaphorical abuse seems like a prescription for great pain. But anyway ... - (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]]; } Here's what I'd try (apologies if you've tried this already; all typed in mail): 1. Create a global variable: MyWindowController* recycledWindowController = nil; 2. In the window controller: - (IBAction)newBookTwo:(id)sender { NSDocumentController *dc = [NSDocumentController sharedDocumentController]; NSURL *url = [NSURL fileURLWithPath:[@~/Desktop/File 2.binary stringByExpandingTildeInPath]]; recycledWindowController = self; [self.document removeWindowController: self]; NSError *error; MainWindowDocument *thisDoc = [dc openDocumentWithContentsOfURL:url display:NO error:error]; } 3. In the document: - (void) makeWindowControllers { if (! recycledWindowController) { recycledWindowController = ... // create a new window controller recycledWindowController.shouldCloseDocument = YES; } [self addWindowController: recycledWindowController]; [recycledWindowController noteChangedDocument]; // or send a notification, or have the window controller KVO-observe its own 'document' property recycledWindowController = nil; } Something like that. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Loading PersistentDocuments into the same window
Think about the Finder. You click on a file, get a preview. If you like it you can open it. That's what I'm driving at. You click on a row in a tableView, you get a preview (the opening is in code, I'm storing an index of file references in a different NSDocument) and if you like it you open up in a new window. The difference here is you can edit the file in preview. I'll try your code. Thanks for the reply. On Apr 30, 2010, at 1:17 PM, Quincey Morris wrote: On Apr 30, 2010, at 08:56, Brad Stone wrote: I want to open NSPersistentDocuments and load them into the same window one at a time. Excuse the soapbox, but you do realize that from the outside looking in, this *seems* like a terrible idea? If your documents really are documents in the user interface sense, then what's the reason for inventing a new document-handling metaphor for users? If they're not really documents, then why try to shoehorn them into the NSDocument metaphor? Keep in mind that Core Data persistent stores don't really fit the standard document metaphor very well, so that NSPersistentDocument is already a little bit broken. (Ask the people who've had trouble with Save As, for example.) Piling on another layer of metaphorical abuse seems like a prescription for great pain. But anyway ... - (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]]; } Here's what I'd try (apologies if you've tried this already; all typed in mail): 1. Create a global variable: MyWindowController* recycledWindowController = nil; 2. In the window controller: - (IBAction)newBookTwo:(id)sender { NSDocumentController *dc = [NSDocumentController sharedDocumentController]; NSURL *url = [NSURL fileURLWithPath:[@~/Desktop/File 2.binary stringByExpandingTildeInPath]]; recycledWindowController = self; [self.document removeWindowController: self]; NSError *error; MainWindowDocument *thisDoc = [dc openDocumentWithContentsOfURL:url display:NO error:error]; } 3. In the document: - (void) makeWindowControllers { if (! recycledWindowController) { recycledWindowController = ... // create a new window controller recycledWindowController.shouldCloseDocument = YES; } [self addWindowController: recycledWindowController]; [recycledWindowController noteChangedDocument]; // or send a notification, or have the window controller KVO-observe its own 'document' property recycledWindowController = nil; } Something like that. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/cocoa-dev%40softraph.com This email sent to cocoa-...@softraph.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Loading PersistentDocuments into the same window
On Apr 30, 2010, at 10:55, Brad Stone wrote: Think about the Finder. You click on a file, get a preview. If you like it you can open it. That's what I'm driving at. You click on a row in a tableView, you get a preview (the opening is in code, I'm storing an index of file references in a different NSDocument) and if you like it you open up in a new window. The difference here is you can edit the file in preview. Ah, I see: the iPad landscape UI collides with the desktop multi-window interface. OK. :) ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Loading PersistentDocuments into the same window
On Apr 30, 2010, at 10:55, Brad Stone wrote: Think about the Finder. You click on a file, get a preview. If you like it you can open it. That's what I'm driving at. You click on a row in a tableView, you get a preview (the opening is in code, I'm storing an index of file references in a different NSDocument) and if you like it you open up in a new window. The difference here is you can edit the file in preview. After letting this percolate for a while ... It's a bit more involved than I thought, because it looks like you actually need 2 window controller classes -- one for the preview, and another for the independently opened documents (assuming those windows don't themselves function as preview windows). In any case, I think I'd keep the initial preview window as a singleton, and always let the document create its own window controller (of a different class, I'd assume), when a file is chosen for preview, but just not show the document's own window yet. Then, I'd attach the singleton controller to the document with addWindowController: (after removing it from another document, if it was previously attached elsewhere). It could could then configure itself to display the document contents. (That would eliminate the global variable and the makeWindowControllers override that I suggested earlier.) That would make it easy to open an independent document window from the preview -- just show its already existing window. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Loading PersistentDocuments into the same window
Yes, I haven't gotten to the second part yet. So, let me see if I understand what you're doing in your first example. You're putting the initial window controller in a global that both the doc and the windowController can get to. When it's time to open a new doc you're saving the current windowController to the global variable, removing it from the dc list and calling [dc openDocument...]. This will reuse the saved global windowController (the code in the document subclass). [dc openDocumentWith...] is opening the new doc and it'll be associated with the same windowController in the makeWindowControllers method. I see how you're doing that. What I don't understand is the documentation for openDocumentWIthCOntentsOfURL doesn't mention anything about setting up the managedObjectContext or creating an undoManager and liking the MOC to a persistentStore. This could just be me because I've been doing cocoa for 6 months (programming in other languages for many years) but are you assuming all the stuff happens because openDocument... is called? Regarding the second part, yes I'm planning on setting up another windowController and when I show the second window moving the managedObjectContext over to it. Both windows will have the same moc. I had a similar problem with that: no undo and the saving didn't work but I put that on hold until I got the first part done. I figure if it works in the preview window making it work in it's own window should be straightforward. On Apr 30, 2010, at 3:36 PM, Quincey Morris wrote: On Apr 30, 2010, at 10:55, Brad Stone wrote: Think about the Finder. You click on a file, get a preview. If you like it you can open it. That's what I'm driving at. You click on a row in a tableView, you get a preview (the opening is in code, I'm storing an index of file references in a different NSDocument) and if you like it you open up in a new window. The difference here is you can edit the file in preview. After letting this percolate for a while ... It's a bit more involved than I thought, because it looks like you actually need 2 window controller classes -- one for the preview, and another for the independently opened documents (assuming those windows don't themselves function as preview windows). In any case, I think I'd keep the initial preview window as a singleton, and always let the document create its own window controller (of a different class, I'd assume), when a file is chosen for preview, but just not show the document's own window yet. Then, I'd attach the singleton controller to the document with addWindowController: (after removing it from another document, if it was previously attached elsewhere). It could could then configure itself to display the document contents. (That would eliminate the global variable and the makeWindowControllers override that I suggested earlier.) That would make it easy to open an independent document window from the preview -- just show its already existing window. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/cocoa-dev%40softraph.com This email sent to cocoa-...@softraph.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Loading PersistentDocuments into the same window
On Apr 30, 2010, at 12:44, Brad Stone wrote: [dc openDocumentWith...] is opening the new doc and it'll be associated with the same windowController in the makeWindowControllers method. I see how you're doing that. What I don't understand is the documentation for openDocumentWIthCOntentsOfURL doesn't mention anything about setting up the managedObjectContext or creating an undoManager and liking the MOC to a persistentStore. This could just be me because I've been doing cocoa for 6 months (programming in other languages for many years) but are you assuming all the stuff happens because openDocument... is called? It happens in the NSPersistentDocument initialization -- the NSDocumentController isn't involved beyond triggering the creation of the document. Regarding the second part, yes I'm planning on setting up another windowController and when I show the second window moving the managedObjectContext over to it. Both windows will have the same moc. I had a similar problem with that: no undo and the saving didn't work but I put that on hold until I got the first part done. I figure if it works in the preview window making it work in it's own window should be straightforward. I believe that undo etc weren't working because your window controller wasn't in the document's windowControllers array. Note that if you're planning on allowing editing in both places, you have a whole other set of issues to do with, to keep the two views of the document contents in sync. I'm back to thinking this might be a bit misguided from the UI point of view. Presumably you have something in the preview window that lets you select which document to preview. Why not just leave that much in the preview window and just let that window open and close real document windows to do the preview? What's the payoff from integrating both into a single window (and is it big enough to justify the development time)? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Loading PersistentDocuments into the same window
Quincey - you were right. The undo wasn't working because the windowController wasn't in the document's windowController array. I used your concept of reusing the same windowController and it works. Thanks! On Apr 30, 2010, at 4:12 PM, Quincey Morris wrote: On Apr 30, 2010, at 12:44, Brad Stone wrote: [dc openDocumentWith...] is opening the new doc and it'll be associated with the same windowController in the makeWindowControllers method. I see how you're doing that. What I don't understand is the documentation for openDocumentWIthCOntentsOfURL doesn't mention anything about setting up the managedObjectContext or creating an undoManager and liking the MOC to a persistentStore. This could just be me because I've been doing cocoa for 6 months (programming in other languages for many years) but are you assuming all the stuff happens because openDocument... is called? It happens in the NSPersistentDocument initialization -- the NSDocumentController isn't involved beyond triggering the creation of the document. Regarding the second part, yes I'm planning on setting up another windowController and when I show the second window moving the managedObjectContext over to it. Both windows will have the same moc. I had a similar problem with that: no undo and the saving didn't work but I put that on hold until I got the first part done. I figure if it works in the preview window making it work in it's own window should be straightforward. I believe that undo etc weren't working because your window controller wasn't in the document's windowControllers array. Note that if you're planning on allowing editing in both places, you have a whole other set of issues to do with, to keep the two views of the document contents in sync. I'm back to thinking this might be a bit misguided from the UI point of view. Presumably you have something in the preview window that lets you select which document to preview. Why not just leave that much in the preview window and just let that window open and close real document windows to do the preview? What's the payoff from integrating both into a single window (and is it big enough to justify the development time)? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/cocoa-dev%40softraph.com This email sent to cocoa-...@softraph.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com