撤消核心数据管理 [英] Undo Management with Core Data

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

问题描述

我试图在iPhone上使用Core Data实现撤消支持,我遇到了一些问题。

I'm trying to implement undo support using Core Data on the iPhone and I ran into a few problems.

我目前有一些管理对象设置但是当我对它们的属性进行更改时,这些更改不会被undo管理器记录。从我的理解,Core Data应该自动设置,我应该能够有更改,创建和删除托管对象的基本撤消和重做支持。

I currently have a couple of managed objects set up but when I make changes to their properties, these changes don't get recorded by the undo manager. From my understanding, Core Data is supposed to have this automatically set up and I should be able to have basic undo and redo support for changes, creation and deletion of managed objects.

是否有特殊的方式来更改对象,以便它们被undo管理器记录?或者,我应该为每个更改注册撤销操作吗?

Is there special way of making changes to the objects so that they get recorded by the undo manager? Or should I be registering undo actions for each change?

此外,假设应用程序滑动到详细视图以编辑特定对象。我想能够撤消所有更改,说,取消按钮被击中。撤销分组在这里适用吗?提交组并让另一个撤消管理器在详细视图中管理更精细的操作与仅使用一个撤销管理器(包含在托管对象上下文中的撤消管理器)有什么不同?

Also, suppose the application slides into a detailed view for editing a specific object. I would like to be able to undo all changes made when say, the cancel button is hit. Would undo grouping be applicable here? What is the difference between committing a group and have another undo manager manage the finer actions in the detailed view versus using just having one undo manager (the one included with the managed object context)?

谢谢!

推荐答案

虽然撤消功能将很有用, NSUndoManager为您想要撤消支持的NSManagedObjectContext。

While the undo features will work pretty much out of the box, you do need to allocate an NSUndoManager for the NSManagedObjectContext for which you want undo support.

最简单的方法是设置undo支持,当有人问你的appDelegate的NSManagedObjectContext

The easiest way to do this is to set up the undo support when something asks your appDelegate for the NSManagedObjectContext

这是苹果给你的默认方法:

This is the default method that apple gives you:

- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];

        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return managedObjectContext;
}

修改它如下所示:

- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];

        //Undo Support
        NSUndoManager *anUndoManager = [[NSUndoManager  alloc] init];
        [managedObjectContext setUndoManager:anUndoManager];
        [anUndoManager release];

        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return managedObjectContext;
}

这篇关于撤消核心数据管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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