How can I cancel a cancel / redo operation?

I have been working with Cocoa for several months, and I am trying to add undo / redo support to the rigorous Cocoa training application that I am writing that allows you to adjust the iTunes track metadata. Thanks to the NSUndoManager method prepareWithInvocationTarget:, I have the basics - you can, for example, undo / undo changes in play counts and recent games with selected tracks. (I use appcript-objc to get / set iTunes track data.)

However, since updating a large number of iTunes tracks may take a little time, I would like to give the user the opportunity to cancel the undo / redo operation during its execution, but I do not see an obvious mechanism for doing this using NSUndoManager. How can i do this?

Edit:
To clarify, thinking about this a bit more, I think that I am actually after this - this is a way to manipulate the undo / redo tables so that I avoid the "inconsistent state" mentioned by Rob Napier in his answer.

So, in response to the canceled operation, which failed without any changes (for example, before calling Undo, the user opened the iTunes settings window, which blocks Apple Events), the cancellation operation may remain at the top of undo, and the repeat stop will remain unchanged. If the operation was canceled or failed in the middle of the stream, then I would like to push the redo operation stack, which cancels the changes that have passed (if any), and have an operation that applies the changes on top of the undo stack, i.e. I suppose that effectively separating an operation between undo and redo tables can cause confusion for the user, but this is perhaps the most forgiving way to solve the problem.

, " ", "" " " " ", .:)

+3
2

, NSUndoManager. , , NSUndoManager . , ( , ). : .

, BOOL, self.shouldContinue. - false ( , - ), .

:

- (void)doOperation
{
    if ([self.thingsToDo count] == 0 || ! self.shouldContinue)
    {
        return;
    }

    id thing = [self.thingsToDo lastObject];
    [self.thingsToDo removeLastObject];

    // Do something with thing

    [self performSelector:@selector(doOperation) withObject: nil afterDelay:0];
}

, . . , , . , . .

+1

. - :

, NSUndoManager .

: http://www.cimgf.com/2008/04/30/cocoa-tutorial-wiring-undo-management-into-core-data/ http://www.mac-developer-network.com/columns/coredata/coredatafeb09/ . , , .

, . , NSUndoManager removeAllActionsWithTarget:

, , . Java, , objective-c, .

, "" ( , Cocoa). , , .

"", , , . , , .

, . UndoWithTarget: selector: object:. , :

invoke(boolean undo) {
    oldUndoManager = currentUndoManager
    setCurrentUndoManager(temporaryUndoManager)

    if (undo)
        temporaryUndoManager.undo()
        oldUndoManager.registerUndo(temporaryUndoManager,
                "invoke", false)
    else
        temporaryUndoManager.redo()
        oldUndoManager.registerUndo(temporaryUndoManager,
                "invoke", true)

    setCurrentUndoManager(oldUndoManager)
}

() / () / . , , .

, . - , . Apple, , .

+1

Source: https://habr.com/ru/post/1721252/


All Articles