Re: grouping undo across method calls in CoreData
Re: grouping undo across method calls in CoreData
- Subject: Re: grouping undo across method calls in CoreData
- From: Jerry Krinock <email@hidden>
- Date: Mon, 5 Oct 2009 14:47:39 -0700
On 2009 Oct 05, at 06:10, Jim Thomason wrote:
But the problem is, if the user undoes the creation of a new object,
two undos are required....
Welcome to what I call "Core Data Undodoodoo" :)
How can I deal with this? I've been trying various combinations of
begin/endUndoGrouping,
The second change to your data model is performed with delay 0.0. So,
try ending your undo group just a little bit later...
[undoManager beginUndoGrouping] ;
[undoManager performSelector:@selector(endUndoGrouping)
withObject:nil
afterDelay:0.01] ;
and even turning on and off groupsByEvent,
Experiments that I have done similar to yours led me to conclude that
Core Data kind-of doesn't like groupsByEvent being turned off. I'm
not really sure, though.
That is, I can hit undo once and delete my object entirely, but then I
can hit undo a second time, and seemingly nothing happens.
Possibly this is because your mortal eyes cannot see the magic that
Core Data is doing and undoing under the hood. Sometimes you can find
out by implementing the Dave Fernandes WhoDooUndo NSLogger, shown
below. If nothing gets logged, it's probably Core Data under-hood.
#if 0
#warning overrode willChangeValueForKey. Do not ship this.
/*!
@brief In a Core Data document-based application, you
often have the problem of the close button getting a dirty
dot immediately after a new document is created or an old
one loaded, or you just want to know what the hell is
happening when you click "Undo" and nothing happens.
Paste this code into the NSManagedObject subclass that you
suspect may be changing, or even better, if you have a
NSManagedObject parent class for all your subclasses
paste it in there. Set #if 1 above, compile,
and run your app so that the dot gets dirty. Then click
Undo until the dot becomes clean as you watch the console.
Any changes to your model will be logged.
@details Thanks to Dave Fernandes for this idea!
*/
- (void)willChangeValueForKey:(NSString*)key {
NSUndoManager* um = [[self managedObjectContext] undoManager] ;
// Since NSUndoManager is not thread-safe,
BOOL isUndoing = [um
performSelectorOnMainThread:@selector(isUndoing)] ;
BOOL isRedoing = [um
performSelectorOnMainThread:@selector(isRedoing)] ;
if (isUndoing || isRedoing) {
NSLog(@"%@ %@did changed value for key: %@",
[[self entity] name],
isUndoing ? @"un" : @"re",
key) ;
// Optional: Put a breakpoint here and debug to see what
caused it
;
}
[super willChangeValueForKey:key];
}
#endif
_______________________________________________
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