Re: registerUndoWithTarget:selector:object: in or outside of group?
Re: registerUndoWithTarget:selector:object: in or outside of group?
- Subject: Re: registerUndoWithTarget:selector:object: in or outside of group?
- From: Graham Cox <email@hidden>
- Date: Fri, 25 Feb 2011 13:41:05 +1100
On 25/02/2011, at 9:05 AM, Jim Thomason wrote:
> This is a braindead simple question, but I couldn't find a definitive reference.
>
> Is it better to register an undo action inside or outside of a group?
>
> i.e., is this preferred:
> []
> Or is this:
>
> [someUndoManager beginUndoGrouping];
> //do interesting things
>
> //first register undo statement
> [someUndoManager registerUndoWithTarget:self @selector(undoSomehow:)
> object:magicalObject];
> //then end group
> [someUndoManager endUndoGrouping];
>
>
> I'd always done it the former way, closing my group first and then
> registering my undo target, and as far as I could tell it worked fine.
> But in trying to debug an issue, I reached a point where I started
> getting exceptions about registering my undo statement when no group
> had been begun. So on a whim, I swapped it to the second method, and
> that seems to have been the cure for what ailed me.
>
> But now I'm trying to confirm if that is a correct fix, or if it's
> just masking some other issue in my code (well, or both, I suppose).
The point of grouping is to collect together related tasks. So if the task is registered outside the group, it's not part of the group.
Probably the reason that it worked in the past is because of the automatic group that is created on each event loop. Because of this loop, it's actually not very common to begin and end groups manually - in fact I've come across very few cases where it's needed. So I wonder whether you're creating groups as a matter of routine when in fact it's not necessary.
If you a) don't disable automatic grouping (i.e. -setGroupsByEvent:NO) and b) carry out all the related activities synchronously within a single event loop, you can forget about dealing with grouping. Note however that the common case of mouseDown/mouseDragged/mouseUp in a view is NOT within a single event loop.
--Graham
_______________________________________________
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