为什么此代码无法清空撤消堆栈
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在禁用和重新启用撤消处理之前,您需要在托管对象上下文上调用 -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.
什么是调用-取消?我遇到过一些其他方法正在注册撤消事件的情况(核心数据可能“有助于”为您执行此操作)。您可能会发现您的撤消堆栈在 -cancel 末尾为空,但获取了调用方法添加的事件。
在这些情况下,选择“撤消”实际上并不会(取消)执行任何操作,该事件没有任何可撤消的内容。为了防止这些多余的事件,您需要在调用方法中禁用撤消注册(然后重新启用它)。在
深入研究之前,请先进行一些简单的 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...
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).