使NSUndoManager忽略NSManagedObject的属性 [英] make NSUndoManager ignore a property of an NSManagedObject

查看:87
本文介绍了使NSUndoManager忽略NSManagedObject的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的主要功能是在图形上排列对象.整个过程都是基于核心数据,并且图中的每个对象都由一个实体表示.对于此示例,该实体将是单元",这是NSManagedObject的自定义子类.

My app's main function is to arrange objects on a plot. The whole thing is based on Core Data, and each object on the plot is represented by an entity. For this example, that entity will be "Unit," a custom subclass of NSManagedObject.

打开绘图时,每个Unit都会创建一个UnitViewController,该KView会观察到Unit属性的更改,例如.positionX,.positionY和.angle. UnitViewController检测到的触摸手势会修改Unit对象,并且视图在观察到它们时会通过移动自身来做出响应.这样可以轻松实现撤消和重做,而无需太多额外的代码,因为当数据库更改时,单元会自行移动.

When the plot is opened, each Unit gets a UnitViewController created, which KVO-observes changes to the Unit's properties, such as .positionX, .positionY, and .angle. Touch gestures that the UnitViewController detect modify the Unit object, and the view responds by moving itself when it observes them. This allows an easy implementation of Undo and Redo without much extra code, where the Units move themselves around when the database changes.

删除单元时会出现问题.

The problem arises when the Unit is deleted.

Unit的一个属性是.viewController,它指向链接到它的UnitViewController.这是一个临时属性,因为显然我不需要它持续存在;每次打开图时,我都会重新创建它. (UnitViewController具有一个指向其Unit的属性.object;本质上是一个相反的属性.)问题是,当我撤消删除或重做创建的撤消操作时,Core Data会尝试还原.viewController属性,但是该UnitViewController对象已被释放.在启用僵尸之前,我一直在获取EXC_BAD_ACCESS,这给了我这个机会:

One property that the Unit has is .viewController, which points to the UnitViewController that's linked to it. It's a transient property, because obviously I don't need it to persist; I re-create it every time I open the plot. (The UnitViewController has a property .object, which points to its Unit; essentially an inverse property.) The problem is, when I Undo a delete, or Redo the Undo of a creation, Core Data tries to restore the .viewController property, but that UnitViewController object has been deallocated. I was getting an EXC_BAD_ACCESS until I enabled zombies, which gave me this:

*** -[UnitViewController retain]: message sent to deallocated instance 0x18365780

所以我的问题是:如何使NSUndoManager完全忽略该属性?我尝试过在修改它之前关闭撤消注册,甚至覆盖了Unit + LD.m(我在NS.ManagedObject子类的Unit.m上的类别)中的setter方法:

So my question is: how can I get the NSUndoManager to completely ignore that property? I've tried turning off undo registration before modifying it, even to the point of overriding the setter method in Unit+LD.m (my category on Unit.m, which is the NSManagedObject subclass):

- (void)setViewController:(UnitViewController *)viewController
{
    [self.managedObjectContext.undoManager disableUndoRegistration];

    [self willChangeValueForKey:@"viewController"];
    [self setPrimitiveValue:viewController forKey:@"viewController"];
    [self didChangeValueForKey:@"viewController"];

    [self.managedObjectContext.undoManager enableUndoRegistration];
}

但是,当我撤消删除操作时,它将尝试恢复.viewController.有人处理过类似的事情吗?有没有更好的方法可以解决这个问题?

But still, it tries to restore the .viewController when I undo a delete. Anyone dealt with anything like this before? Is there a better way to go about this?

推荐答案

我最终向Apple提交了支持票,他们立即返回了一个非常简单的答案:在Unit.m中覆盖setter方法-setViewController:并将其清空. (或者就我而言,在Unit + LD.m中,因为我将所有自定义的东西都放在一个类别中,以避免在更新数据模型时必须重新执行它.)

I ended up submitting a support ticket to Apple, and they came back immediately with a pretty simple answer: override the setter method -setViewController: in Unit.m and make it empty. (Or in Unit+LD.m, in my case, since I do all my custom stuff in a category to avoid having to re-do it when I update my data model.)

- (void)setViewController:(UnitViewController *)viewController {

}

我只在代码中设置了几次该属性,然后,我只使用-setPrimitiveValue:forKey:.我仍然需要进行一些测试,以确保其他所有功能都可以正常运行,但就目前而言,它似乎像是一种魅力.撤消管理器从不尝试恢复该属性,而且我已停止崩溃.

I only set the property a couple times in the code, and when I do, I just use -setPrimitiveValue:forKey: instead. I still have some testing to do to make sure everything else works, but for now, it seems to work like a charm. The undo manager doesn't ever try to restore the property and I've stopped getting crashes.

这篇关于使NSUndoManager忽略NSManagedObject的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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