Synchronising across multiple NSUndoManager stacks
Synchronising across multiple NSUndoManager stacks
- Subject: Synchronising across multiple NSUndoManager stacks
- From: Graham Cox <email@hidden>
- Date: Sat, 22 Mar 2008 11:50:35 +1100
This may not be entirely a Cocoa question as much as it is general in
nature... but any insight would be appreciated.
I have a multi-document Cocoa app where each document has its own
NSUndoManager, as per normal. Undo works great in general. However I
have a particular class of object, we'll call it 'S' whose instances
's' can be shared by objects across more than one document. This is
intentional - the idea is to have properties of 's' applied to all
objects that use 's' at once, so if I change a property of 's' every
client object using 's' updates, even across multiple documents. That
also works great.
The problem comes in with handling undo. The property changes of 's'
need to be undoable, so that the undo stacks of every document
correctly work with respect to 's' - and so when a change in 's' is
undone, it is seen by any other documents using 's' as a do.
I've tried to solve this a few ways, for example:
1. Each instance of 's' keeps a list of the undo managers of all its
client objects. Using an NSProxy subclass to stand in for a single
undo manager, the undoes are pushed out to every undo manager in the
list. This seemed to work sort of OK, but when a document is closed I
ran into problems in cleaning up the list of undo managers in the
proxies (bearing in mind there can be many instances). Since I wasn't
convinced this was the right solution anyway I decided to try other
approaches.
2. Each client of 's' saves an undo task standing in for the "real
thing" which is saved on a separate undo stack global to all instances
of 's'. When the proxy undo is undone, it undoes the global stack as
well, but that change is seen by other documents as a DO. The logic
needed to keep this in any sort of sync quickly made my head swim.
When that happens I always think that's gotta be the wrong way!
3. Each instance 's' has its own undo stack using the proxy approach
of (2). I haven't actually tried this for real yet, just as a thought
experiment. It seems as if it could work on paper but it sounds a bit
weird to have to have separate undo stacks for every instance of 's'.
Also this has ownership issues - who owns the stack? 's'? That's a
retain cycle... How are deletes of 's' undone?
4. Simply prevent instances of 's' being shared across documents. This
is the sanest in some ways but the ability to share 's' across
documents is an important feature of the application that is required
by the spec.
So the question is, is this a requirement that others have come across
and if so, how did you solve it?
-----
S.O.S
_______________________________________________
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