Re: CoreData and undo/redo : how to add a managed object with attributes already set in the undo/redo stack ?
Re: CoreData and undo/redo : how to add a managed object with attributes already set in the undo/redo stack ?
- Subject: Re: CoreData and undo/redo : how to add a managed object with attributes already set in the undo/redo stack ?
- From: Jerry Krinock <email@hidden>
- Date: Sat, 26 Jun 2010 19:08:29 -0700
On 2010 Jun 26, at 16:01, Guillaume Laurent wrote:
> I'm having difficulties with the undo/redo mechanism and my Core Data objects.
Uh-huh.
> The problem is that I create a CoreData object (say a rectangle), then set some of its attributes according to some controls values (the position and size of a CALayer the user has just created in a view). This set of actions is a single one from the user's point of view. I know I can make it appear so with NSUndoManager:beginUndoGrouping/endUndoGrouping. But when I undo this in the app, each of the steps of the object's creation are undone seperately, including the setting of each of the attributes. ....
> Bottom line : is there a way to make it so that the CoreData object's instantiation and its setup are registered as a single action in the undo/redo stack ?
I presume that you are setting these attributes in -awakeFromInsert. One solution I've seen used for this particular problem is to disable undo registration when the attributes are set. Something like this:
- (void)awakeFromInsert {
[super awakeFromInsert] ;
// Close the curtain so the wizard can do some magic
[[self managedObjectContext] processPendingChanges] ;
[[[self managedObjectContext] undoManager] disableUndoRegistration] ;
// Do some magic
[self setFoo:whatever] ;
[self setBar:whateverElse] ;
// Re-open the curtain
[[self managedObjectContext] processPendingChanges] ;
[[[self managedObjectContext] undoManager] enableUndoRegistration] ;
}
So the setting of foo and bar aren't even on the undo stack. But when you Undo, it removes the object, so no one cares.
A simpler solution, if the values are always the same, is to set default values in your data model.
There are other issues with Core Data and Undo that you can read about in the list archives. Maybe you haven't run into these yet ;) For starters, any time you want to get serious about Undo, you should use Graham Cox' GCUndoManager instead of NSUndoManager. Besides fixing an annoying bug or two in NSUndoManager, it pulls back the covers so you can debug undo issues.
http://apptree.net/gcundomanager.htm
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden