Re: CoreData based document NSUndoManager troubles
Re: CoreData based document NSUndoManager troubles
- Subject: Re: CoreData based document NSUndoManager troubles
- From: Bill Cheeseman <email@hidden>
- Date: Wed, 17 Aug 2005 06:58:46 -0400
on 2005-08-17 1:45 AM, Colin Cornaby at email@hidden wrote:
> At init time for my NSPersistentDocument I clear the undo stack after
> I do setup. This works fine. The stack is cleared and the document
> appears as not modified. The code I use is:
>
>
> [[self managedObjectContext] processPendingChanges];
> [[[self managedObjectContext] undoManager] removeAllActions];
> [self updateChangeCount:NSChangeCleared];
There's another way to do this which is, I believe, a little more efficient.
Instead of allowing undo actions to be registered and then removing them,
bracket your initialization code in invocations of [context
disableUndoRegistration] and [context enableUndoRegistration], and be sure
to invoke [context processPendingChanges] just before re-enabling undo
registration. The invocation of -processPendingChanges will prevent the
changes from being processed later, at the end of the run loop, after you
have re-enabled undo registration.
> I then use the following code within an NSManagedObject subclass when
> it is modified:
>
> [[[self managedObjectContext] undoManager] registerUndoWithTarget:self
> selector:@selector(setResourceData:)
> object:[self valueForKey:@"data"]];
> [[[self managedObjectContext] undoManager] setActionName:@"Edit
> Resource"];
You normally shouldn't have to register changes with the undo manager
yourself, nor to updateChangeCount: yourself. You should only need to set
the action name yourself. The rest should just happen, assuming you've set
everything else up correctly. Trying to do it manually will presumably mess
things up internally.
> However, this code doesn't enable the undo menu item. I did read that
> I may need to do an updateChangeCount on the NSDocument, but this
> raises a few issues. First, I thought if I was using the NSDocument's
> undoManager (which is shared by the managedObjectContext) that I
> wouldn't need to use updateChangeCount. Secondly, the CoreData code
> shouldn't really be talking to something as higher than it as the
> NSPersistentDocument. Have I done something wrong? Is there any way I
> can get this working without having my schema classes talk to the
> NSDocument classes?
There must be some other problem in your code. Are you redefining the undo
manager somewhere? Again, you should normally just let NSPersistentDocument
do this for you -- assuming you're using NSPersistentDocument.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
PreFab Software - http://www.prefab.com/scripting.html
The AppleScript Sourcebook - http://www.AppleScriptSourcebook.com
Vermont Recipes - http://www.stepwise.com/Articles/VermontRecipes
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden