Preserving undo actions on deleted targets
Preserving undo actions on deleted targets
- Subject: Preserving undo actions on deleted targets
- From: Matthew LeRoy <email@hidden>
- Date: Wed, 29 Jan 2014 15:30:05 +0000
- Thread-topic: Preserving undo actions on deleted targets
I’m wondering if someone can provide some guidance or best practices on how I might preserve actions on the undo/redo stack when the target of those actions is deleted?
For example, say I have an application that allows me to add and remove records to and from a list — employee records, items in an inventory control system, anything really. I have model objects which represents each record, and those model objects register undo actions whenever their attributes/properties are changed. (I’m fairly certain this is the recommended best practice.)
I also want to be able to delete records, and to be able to undo that deletion. The Undo Architecture documentation clearly warns not to leave actions on the undo stack whose targets have been deallocated, and points to removeAllActionsWithTarget:. OK, so when I delete a record and thus delete the corresponding model object, I just call removeAllActionsWithTarget:modelObject — easy. But then I realize that now any undo actions that targeted that object are gone, and even if I undo the deletion I can’t keep undo-ing any changes which were made to that object prior to it being deleted (e.g. undo changing the name of an employee, changing the unit cost of an inventory item, etc).
I *think* the right approach here is to not delete/dealloc the model objects when their corresponding records are removed/deleted, but instead just remove the objects from the collection of “live” objects and hold onto them somewhere else for the purposes of re-inserting them in the collection if the deletion is undone. I suppose my model code for deleting a record from the live collection should register an undo action which calls “undelete” or “insert”; and the code for “undelete” or “insert” (and probably “add” as well) should register an undo action which calls “delete”?
I also shouldn’t call removeAllActionsWithTarget:modelObject, because there’s no way to re-insert those actions back into the undo stack if the deletion is undone and the model object is re-inserted back into the “live” collection. Instead, I just have to trust that the only way for the undo actions which target the “deleted” model object to get sent is for the undo action which undeletes the object to be sent first (because it is higher on the undo stack), thus putting the object back into the live collection. I don’t need to worry about undo actions getting sent to deallocated objects, because the objects aren’t getting deallocated.
Am I on the right track, or is there some other recommended pattern here?
Matt
_______________________________________________
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