2

I have a popover that opens and closes. When it opens, I call -beginUndoGrouping on my NSUndoManager. When it closes, I call -endUndoGrouping.

If I view my undo stack after the fact, I see this:

0: endUndoGrouping
1: endUndoGrouping
2: beginUndoGrouping
3: beginUndoGrouping

Note that there are no actions added inbetween the popover opening and closing. I assume the grouping stays around because there is a nested undo group?

But I searched everywhere, and I am 100% positive I am not creating the nested group myself. Here is my code:

- (void)colorWellWillShow:(NSNotification*)notification
{
   NSLog(@"HEYHEY");
   NSLog(@"GROUP BY EVENT?: %@", [[[[self imageController] document] undoManager] groupsByEvent] ? @"YES": @"NO");
   NSLog(@"GROUPING LEVEL: %ld", (long)[[[[self imageController] document] undoManager] groupingLevel]);
   [[[[self imageController] document] undoManager] beginUndoGrouping];
   NSLog(@"GROUP BY EVENT?: %@", [[[[self imageController] document] undoManager] groupsByEvent] ? @"YES": @"NO");
   NSLog(@"GROUPING LEVEL: %ld", (long)[[[[self imageController] document] undoManager] groupingLevel]);
}

- (void)colorWellDidClose:(NSNotification*)notification
{
   NSLog(@"GROUP BY EVENT?: %@", [[[[self imageController] document] undoManager] groupsByEvent] ? @"YES": @"NO");
   NSLog(@"GROUPING LEVEL: %ld", (long)[[[[self imageController] document] undoManager] groupingLevel]);
   [[[[self imageController] document] undoManager] endUndoGrouping];
   NSLog(@"GROUP BY EVENT?: %@", [[[[self imageController] document] undoManager] groupsByEvent] ? @"YES": @"NO");
   NSLog(@"GROUPING LEVEL: %ld", (long)[[[[self imageController] document] undoManager] groupingLevel]);
}

Here is the output when I reproduce:

2015-12-21 15:37:41.300 Snagit[18714:8416571] HEYHEY 2015-12-21 15:37:41.300 Snagit[18714:8416571] GROUP BY EVENT?: YES 2015-12-21 15:37:41.300 Snagit[18714:8416571] GROUPING LEVEL: 0 2015-12-21 15:37:41.301 Snagit[18714:8416571] GROUP BY EVENT?: YES 2015-12-21 15:37:41.301 Snagit[18714:8416571] GROUPING LEVEL: 2 2015-12-21 15:37:45.000 Snagit[18714:8416571] GROUP BY EVENT?: YES 2015-12-21 15:37:45.000 Snagit[18714:8416571] GROUPING LEVEL: 1 2015-12-21 15:37:45.001 Snagit[18714:8416571] GROUP BY EVENT?: YES 2015-12-21 15:37:45.001 Snagit[18714:8416571] GROUPING LEVEL: 0

Based on this, it looks like when I call -beginUndoGrouping, it immediately pushes 2 "beginUndoGrouping" actions onto the stack.
It would appear that any action I add at this point will go into the inner group.

Then directly before I call -endUndoGrouping, it magically thinks that there is only one undo group, so I can only assume any actions added at this point would go into the outer group.
And then after the call it thinks that I am back at the bottom level, and will add actions outside of the group.

So my question is, why are two groups being pushed onto my undo stack when I am only calling -beginUndoGrouping once? And how can I remove all of the groups from my undo stack if there are no actions inside of the group? I believe this should happen automatically, but again, because there is a nested undo group inside of the outer group-- the outer group does not automatically resolve.

A O
  • 5,516
  • 3
  • 33
  • 68
  • The undo manager also groups undo's. If you close the undo group in colorWellWillClose you probably will see level 2 and 1. What happens if you don't begin and end undo grouping? – Willeke Dec 21 '15 at 21:45
  • If I do not begin/end grouping, then nothing is ever pushed onto the stack (assuming I am following the same reproduction steps, where I just open the popover and close it without adding any actions) – A O Dec 21 '15 at 21:57
  • If I do not begin/end grouping, and add an action to the stack, it gets added with only a "beginUndoGrouping" and no matching "endUndoGrouping". This throws an exception when I try to undo – A O Dec 21 '15 at 21:58
  • NSUndoManager 0xcea2d60 is in invalid state, must begin a group before registering undo. – A O Dec 22 '15 at 02:11
  • Did you see this answer? [NSUndoManager : separate multiple changes in one run loop cycle](http://stackoverflow.com/questions/16763716/nsundomanager-separate-multiple-changes-in-one-run-loop-cycle) – Willeke Dec 24 '15 at 01:09

0 Answers0