NSUndoManager、核心数据和选择性撤消/重做

发布于 2024-10-09 11:51:02 字数 546 浏览 4 评论 0原文

我正在开发一个核心数据应用程序,该应用程序具有相当大的托管对象层次结构,类似于树。

创建基础对象时,它会创建一些子对象,这些子对象又创建自己的子对象,依此类推。这些子对象中的每一个都可以使用 NSURLConnections 收集信息。

现在,我想使用 ManagedObjectContext 中的 undoManager 支持撤消/重做。问题是,如果用户创建一个基础对象,然后尝试撤消该操作,则该基础对象不会被删除。相反,可以移除一个或多个子对象。显然,这种类型的行为是不可预测的并且是不需要的。

所以我尝试默认禁用撤消注册。我通过在 ManagedObjectContext 中进行任何修改之前调用 disableUndoRegistration: 来完成此操作。然后,在创建基础对象等基本操作之前启用撤消注册,然后再次重新禁用注册。

现在,当我尝试撤消时,我收到此错误:

撤消:NSUndoManager 0x1026428b0 位于 无效状态,调用撤消 嵌套撤消组过多

有想法吗?

I'm working on a core data application that has a rather large hierarchy of managed objects similar to a tree.

When a base object is created, it creates a few child objects which in turn create their own child objects and so on. Each of these child objects may gather information using NSURLConnections.

Now, I'd like to support undo/redo with the undoManager in the managedObjectContext. The problem is, if a user creates a base object, then tries to undo that action, the base object is not removed. Instead, one or more of the child objects may be removed. Obviously this type of action is unpredictable and unwanted.

So I tried disabling undo registration by default. I did this by calling disableUndoRegistration: before anything is modified in the managedObjectContext. Then, enabling undo registration before base operations such as creating a base object the again re-disabling registrations afterwords.

Now when i try to undo, I get this error:

undo: NSUndoManager 0x1026428b0 is in
invalid state, undo was called with
too many nested undo groups

Thoughts?

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

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

发布评论

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

评论(3

故人爱我别走 2024-10-16 11:51:03

NSUndoManager 等待下一个运行循环周期,直到它注册您的更改

// do your stuff

// give the run loop a breath

[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:[NSDate date]];
[undoManager disableUndoRegistration];

NSUndoManager waits for the next run loop cycle until it registers your changes

// do your stuff

// give the run loop a breath

[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:[NSDate date]];
[undoManager disableUndoRegistration];
裂开嘴轻声笑有多痛 2024-10-16 11:51:03

自从这个问题发布以来已经一年多了,但无论如何这里有一个答案:

你应该查看苹果的文档,它说:

.. 撤消消息关闭最后一个打开的撤消组,然后应用该组中的所有撤消操作...如果调用撤消时堆栈上有任何未关闭的嵌套撤消组,则会引发异常。要撤消嵌套组,您必须使用 endUndoGrouping 消息显式关闭该组,然后使用 undoNestedGroup 撤消它。

http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/UndoArchitecture/Articles/PerformingUndo.html

More than a year since this question was posted but anyway here's a answer:

Your should check out apple's Documentation it says:

.. The undo message closes the last open undo group and then applies all the undo operations in that group ... If any unclosed, nested undo groups are on the stack when undo is invoked, it raises an exception. To undo nested groups, you must explicitly close the group with an endUndoGrouping message, then use undoNestedGroup to undo it.

http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/UndoArchitecture/Articles/PerformingUndo.html

荒芜了季节 2024-10-16 11:51:03

我与 NSUndoManager 的交集处于无效状态,调用撤消时使用了太多嵌套撤消组 不涉及 CoreData,但我的答案可能仍然有用。

在我的例子中,撤消管理器异常是由于我的代码中在调用 NSUndoManager -undo 期间引发的未捕获异常而引发的。

回顾控制台,我可以看到我的应用程序代码异常和撤消管理器的 NSInternalInconsistencyException。

我使用了默认的运行循环撤消组行为,并且没有明确对我的撤消注册进行分组。

My intersection with NSUndoManager is in invalid state, undo was called with too many nested undo groups did not involve CoreData however my answer may be useful none the less.

In my case this the undo manager exception was raised due to an uncaught exception in my code that was raised during a call to NSUndoManager -undo.

Looking back through the console I could see both my app code exception and the undo manager's NSInternalInconsistencyException.

I used the default runloop undo group behaviour and did not explicit group my undo registrations.

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