title: How to handle undoing actions within a group, before ending the group
body: <p>I have several undo actions that I want to group.</p>
  
  <p>The user will enter a state in the application, and start modifying objects.<br>
  I need a <code>beginUndoGrouping</code> to start,<br>
  Then every time an object is modified, an action is added to the stack.  </p>
  
  <p>When the user exits this state, I need <code>endUndoGrouping</code>.
  So when outside of this specific state, the user can undo all actions at once.</p>
  
  <p>The problem is, <strong>while</strong> they are in this state, I would also like the user to be able to undo each action individually.</p>
  
  <p>This is a problem because I already began the undo grouping,<br>
  I get this exception every time the <code>-undo</code> message is sent:</p>
  
  <p><code>2016-03-14 16:42:08.044 Snagit[6778:7933371] undo: NSUndoManager 0x610000286ef0 is in invalid state, undo was called with too many nested undo groups</code></p>
  
  <p>The way I'm seeing it, I have a few options.</p>
  
  <p><em>1) Find a way to undo the last top-level action without undoing the parent group (According to the APIs this would in theory be possible by calling <code>undoNestedGroup</code>.  However, it's unclear how I would implement this solution.</em><br>
  <strong>EDIT</strong> After some testing, I don't think this option will work.  I tried programmatically calling <code>undoNestedGroup</code> before ending the undo group, and I still got the exception</p>
  
  <p>2) Live with the exception being thrown. It doesn't cause a crash, and an undo doesn't happen.  The undo stack does not change when this exception gets thrown, so it doesn't appear to be an issue.</p>
  
  <p>3) Rethink how I am grouping undo actions.  An example would be perhaps keeping track of the number of actions being put on the undo stack.  Don't begin the undo group at the beginning, but rather, at the end, somehow manually create a new undo group and place the appropriate number of undo actions into the group.  This solution seems horrible though.</p>
  
  <p><strong>EDIT2</strong>: Here is the undo stack when I <code>undo</code> and the exception is thrown:</p>
  
  <pre><code>0: target: ImageController 0x608000168b80 -- selector:setCanvasRect:
  1: endUndoGrouping
  2: beginUndoGrouping  
  3: beginUndoGrouping  
  </code></pre>
  
  <p>Note there is an empty group, perhaps that could cause the issue but I really don't see how.  Shouldn't <code>undo</code> add the necessary <code>endUndoGrouping</code> and it would be good?</p>
  
  <p><strong>EDIT3</strong> 
  Settings <code>groupsByEvent</code> to <code>NO</code> removes that empty group.</p>
  
  <p>Undo stack now looks like this:</p>
  
  <pre><code>0: target: ImageController 0x600000164800 -- selector:setCanvasRect:
  1: beginUndoGrouping  
  </code></pre>
  
  <p>when undo is pressed, still get the exception</p>
  
  <pre><code>2016-03-15 16:40:49.560 Snagit[78079:8308950] undo: NSUndoManager 0x608000097a70 is in invalid state, undo was called with too many nested undo groups
  </code></pre>
  
  <p>So it seems I am missing something that makes <code>undo</code> automatically end the undo group?</p>