Re: Core Data undo grouping
Re: Core Data undo grouping
- Subject: Re: Core Data undo grouping
- From: Mike Abdullah <email@hidden>
- Date: Fri, 3 Oct 2008 12:02:18 +0100
I agree with Ben that this seems somewhat odd to do, but that said:
NSManagedObjectContext coalesces changes for registering with the undo
manager, but NSUndoManager also performs its own grouping. -
processPendingChanges just provides a means for forcing the MOC to
register its changes when you want. You still need to close off the
undo managers grouping to match. So do something like ths:
[MOC processPendingChanges];
usnigned undoGrouping = 0;
while ([[MOC undoManager] groupingLevel] > 0)
{
[[MOC undoManager] closeUndoGrouping];
undoGrouping++;
}
while ([[MOC undoManager] groupingLevel] < undoGrouping)
{
[[MOC undoManager] beginUndoGrouping];
}
On 2 Oct 2008, at 22:16, Peter Sagerson wrote:
I'm using CoreData for some internal state management that sometimes
requires an undo boundary in a specific place. In other words, I
need something along these lines to work:
NSManagedObject *object = [self getObjectFromSomewhere];
NSManagedObjectContext *context = [object managedObjectContext];
[object setValue:@"1" forKey:@"attr"];
[self forceUndoBoundaryInContext:context];
[object setValue:@"2" forKey:@"attr"];
[context undo];
STAssertEqualObjects([object valueForKey:@"attr"], @"1", @"");
Based on the documentation, it seems clear that all I have to do to
accomplish this is call processPendingChanges:
- (void)forceUndoBoundaryInContext:(NSManagedObjectContext *)context
{
[context processPendingChanges];
}
But this does not work. I installed notification handlers for
NSUndoManagerDidOpenUndoGroupNotification and
NSUndoManagerWillCloseUndoGroupNotification and I can see that the
group is not closed until I call undo. As a workaround, I've found
that I can fiddle with the context's undo manager directly:
- (void)forceUndoBoundaryInContext:(NSManagedObjectContext *)context
{
[context processPendingChanges];
[[context undoManager] endUndoGrouping];
[[context undoManager] beginUndoGrouping];
}
This works, but it seems kind of sneaky and underhanded and I'm not
entirely comfortable with it. Is there a better way to do this? Does
anyone else find this behavior inconsistent with the documentation
for processPendingChanges?
Thanks
_______________________________________________
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
_______________________________________________
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