couchbase/couchbase-lite-ios — pull request #166 by Zymantas (2013-10-23 12:53:49+00:00)
Title: NSUndoManager support for CBLModels

So, at last I had some time to implement NSUndoManager for CBLModels. Here is what I did:
* One change is registered per each undo group. Undo groups can be created by user or automatically (NSUndoManager automatically creates new one each run-loop cycle if -[NSUndoManager groupsByEvent] == YES). After receiving NSUndoManagerDidCloseUndoGroupNotification notification, each model is marked again as "_canUndo = true".
* If model gets deleted, I retain self (model) and then reuse same model when undoing delete action by calling -[self undoDeleteModel:]. I need this because user could perform some edits to the model, then delete it. If I will create new model, then following undo edit operations will not have a reference to recreated model. However, this looks strange to me (retaining self so that I could call self again). I thought about moving handling delete actions to CBLDatabase but this creates new problem, if there will be external changes I won't be able to remove undo actions only for that model.
* Updated Model Tests to include test case for undo/redo actions.
* Updated OSX Demo.

Some notes:
* I am not sure if catching created model and registering undo action at setDocument: method is a good choice.
* If model wasn't saved before deleting it, properties were not cleared. I've fixed this one.
* If model wasn't saved before deleting it, CBLDocument wasn't detached from the model. I've fixed this, however I am not sure whether this was intentional or not.
* When CBLDocument is changed externally, -[CBLModel tdDocumentChanged:] is being called where I discard all undo information for the particular model, so user will not undo synced changes by accident. However, calling -[CBLModel deleteDocument:] triggers -[CBLModel tdDocumentChanged:] as well. I've added additional private variable "_deleting" to CBLModel in order to workaround this side effect.
* If attachments will be implemented as properties like in OSX Demo app (ShoppingItem.picture property) undo actions won't be visible to user. How can we fix this? Some sort of a notifications of what has been undone/redone?

I will provide fixes in case I will find bugs with this implementation.
Any comments about what I did are more than welcome :)