带有CoreData的嵌套撤消组 [英] Nested undo group with CoreData

查看:293
本文介绍了带有CoreData的嵌套撤消组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在coredata支持的iphone应用程序中添加撤消管理器。当用户尝试添加新对象时(通过点击+按钮),我加载一个新的模态视图控制器并在viewDidLoad中启动一个新的撤消组。

I want to add a undo manager to a coredata backed iphone application. When the user tries to add a new object (by tapping on + button) I load a new modal viewcontroller and start a new undo group in viewDidLoad.

当用户按下时取消按钮,我想回滚cancelAction回调中的更改。

When the user presses Cancel button, I want to rollback the changes in cancelAction callback.

查询:


  1. 是否可以启动嵌套的撤消组并通过事件循环将其保留并整理在一个撤消组中完成的所有更改?现在,当我在cancelAction中调用beginUndoGrouping时,我得到一个缺少的beginUndoGrouping异常。

  2. groupsByEvent API对NSUndoManager的意义是什么?我是否需要将其设置为NO以通过事件循环保留撤消组?

  3. 在saveAction回调中需要做什么才能永久更改(除了在mangedObjectContext上调用save)?如何告知撤消管理器停止跟踪更改?

错误消息:

* 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'_endUndoGroupRemovingIfEmpty :: NSUndoManager 0x75415f0处于无效状态,调用endUndoGrouping时没有匹配的开始

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '_endUndoGroupRemovingIfEmpty:: NSUndoManager 0x75415f0 is in invalid state, endUndoGrouping called with no matching begin

示例代码:

// RootViewController.m
- (void) addAction {
   // Load Modal View Controller to add new object

}

// AddViewController.m
- (void) viewDidLoad {
   // Start nested undo group
   [self.managedObjectContext processPendingChanges];
   [self.managedObjectContext.undoManager beginUndoGrouping];

   [self createModel];

}

- (void) cancelAction {
    // Revert all changes
    [self.managedObjectContext processPendingChanges];
    [self.managedObjectContext.undoManager endUndoGrouping];

    [self.managedObjectContext.undoManager undoNestedGroup];

    ...
}

- (void) saveAction {

    // Save changes
}


推荐答案

从您的具体问题开始 - 是的,您可以手动定义界限使用beginUndoGrouping和endUndoGrouping进行撤消操作。

Beginning with your specific questions - Yes, you can manually define the bounds of an undo operation using beginUndoGrouping and endUndoGrouping.

在这种情况下,无论是否设置groupsByEvent,撤消操作都应该有效。这是因为事件循环生成的所有撤消组都嵌套在以beginUndoGrouping开头的主要开放撤消分组下,并且只要在调用endUndoGrouping后直接调用undoNestedGroup,它就应该有效。如果您没有使用事件循环撤消分组,请不要担心并将其设置为NO。

In this case, the undo operation should work whether or not groupsByEvent is set. This is because all the undo groups that are generated by the event loop are nested under your main open undo grouping started with beginUndoGrouping, and as long as you call undoNestedGroup directly after you call endUndoGrouping, it should work. If you are not using the event loop undo groupings, don't worry about it and set it to NO.

要使更改成为永久更改,请使用endUndoGrouping关闭撤消组并调用保存您的上下文。不需要processPendingChanges调用,并且可能会导致嵌套组出现问题。如果要清除撤消操作,请在endUndoGrouping之后调用undomanager上的removeAllActions - 这可以保证更改永远不会取消。

To make your changes permanent, close the undo group with endUndoGrouping and call save on your context. The processPendingChanges calls are not needed, and may cause issues in nested groups. If you want clear the undo operations, call removeAllActions on your undomanager after endUndoGrouping - this guarantees the changes will never be un-did.

使用断点/ nslog确保开始/结束通话是一对一的。

Use breakpoints/nslog to make sure your begin/end calls are one for one.

如果您希望取消操作就像撤消按钮,则必须执行以下操作:

If you want your cancel action to be like an 'undo button', you'll have to do the following:


  • 将beginUndoGrouping移至viewWillAppear

  • Move beginUndoGrouping to viewWillAppear

在viewWillDisappear中调用endUndoGrouping

Call endUndoGrouping in viewWillDisappear

在取消操作结束时重新打开撤消分组

re-open undo grouping at the end of your cancel action

在保存操作结束时重新打开撤消分组

re-open undo grouping at the end of your save action

否则,如果保持原样,请确保在保存和取消操作中关闭对话框,避免多次调用endUndoGrouping的可能性。

Otherwise, if you leave it as is, make sure you close the dialog in your save and cancel actions, to avoid possibility of endUndoGrouping being called multiple times.

如果您有任何疑问,请发表评论,我会更新。

If you have any questions, please comment and I'll update.

祝你好运!

这篇关于带有CoreData的嵌套撤消组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆