为什么此代码无法清空撤消堆栈

发布于 2024-08-08 11:43:53 字数 424 浏览 6 评论 0原文

- (void)cancel {
//   [managedObjectContext.undoManager disableUndoRegistration];
     [managedObjectContext deleteObject:object]; // I don't want this deletion to be recorded on the undo stack which is empty at this point.
//   [managedObjectContext.undoManager enableUndoRegistration];
     [managedObjectContext.undoManager removeAllActions];
}

使用这段代码,我仍然在撤消堆栈上进行了删除,即使取消注释这两行也不能阻止记录。为什么?

- (void)cancel {
//   [managedObjectContext.undoManager disableUndoRegistration];
     [managedObjectContext deleteObject:object]; // I don't want this deletion to be recorded on the undo stack which is empty at this point.
//   [managedObjectContext.undoManager enableUndoRegistration];
     [managedObjectContext.undoManager removeAllActions];
}

With this code, I still got the deletion on the undo stack, even uncommenting those two lines can not prevent the recording. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

著墨染雨君画夕 2024-08-15 11:43:53

在禁用和重新启用撤消处理之前,您需要在托管对象上下文上调用 -processPendingChanges。 Core Data 延迟注册撤消事件,直到 processPendingChanges,以允许合并对同一属性和类似快捷方式的多个更改。

You'll need to call -processPendingChanges on the managed object context before disabling and before re-enabling undo processing. Core Data delays registering undo events until processPendingChanges to allow for coalescing multiple changes to the same attribute and similar shortcuts.

彼岸花似海 2024-08-15 11:43:53

什么是调用-取消?我遇到过一些其他方法正在注册撤消事件的情况(核心数据可能“有助于”为您执行此操作)。您可能会发现您的撤消堆栈在 -cancel 末尾为空,但获取了调用方法添加的事件。

在这些情况下,选择“撤消”实际上并不会(取消)执行任何操作,该事件没有任何可撤消的内容。为了防止这些多余的事件,您需要在调用方法中禁用撤消注册(然后重新启用它)。在

- (void)doSomething {
    // Disable undo until we definitely want to undo stuff
    [managedObjectContext.undoManager disableUndoRegistration];

    // Stuff goes here

    if ( userCancelled ) {
        [self cancel];
        [managedObjectContext.undoManager enableUndoRegistration];
        return;
    }

    // We actually want user to be able to undo the following stuff
    [managedObjectContext.undoManager enableUndoRegistration];

    // More stuff goes here
}

深入研究之前,请先进行一些简单的 NSLog 检查,以查看撤消堆栈上的内容(并确保您将消息发送到真实的 NSUndoManager 而不是 nil 对象)。

What is calling -cancel? I have encountered situations where some other method is registering an undo event (and core data may be "helpfully" doing this for you). You may find that your undo stack is empty at the end of -cancel but gets an event added by the calling method.

In these cases choosing "Undo" doesn't actually (un)do anything, the event has nothing to undo. To prevent these superfluous events you need to disable undo registration (and then reenable it) in the calling method. Something along the lines of...

- (void)doSomething {
    // Disable undo until we definitely want to undo stuff
    [managedObjectContext.undoManager disableUndoRegistration];

    // Stuff goes here

    if ( userCancelled ) {
        [self cancel];
        [managedObjectContext.undoManager enableUndoRegistration];
        return;
    }

    // We actually want user to be able to undo the following stuff
    [managedObjectContext.undoManager enableUndoRegistration];

    // More stuff goes here
}

Before delving too far into this do some simple NSLog checks to see what is on the undo stack when (and to make sure you are sending messages to a real live NSUndoManager rather than a nil object).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文