禁用撤消创建/删除 NSManagedObject
在我的核心数据模型中,我有一个名为 listItems
的关系,它链接到多个 listItem
实体,每个实体都有一个 stringValue
属性。 我创建了一个控件,它本质上是一个 NSTextFields 列表,每个列表项一个。 该控件已正确绑定到 listItems
,并且我已将其设置为按回车键会在当前编辑的字段正下方创建一个新字段,并将焦点更改为新字段。 因此,本质上,要添加新项目,用户需要按 Return 键。
同样,如果用户结束编辑并且当前编辑的字段为空,则该字段将被删除(可以说,空字段仅在“编辑模式”期间出现)。 这效果非常好。 基本上,在我的 listItem
NSManagedObject 子类中,我执行以下操作:
// Don't allow nil values
if (!value && [[self.recipe ingredients] count] > 1) {
for (EAIngredientRef *ingredient in [self.recipe ingredients]) {
if ([[ingredient sortIndex] integerValue] > [[self sortIndex] integerValue]) {
[ingredient setSortIndex:[NSNumber numberWithInteger:([[ingredient sortIndex] integerValue]-1)]];
}
}
[[self managedObjectContext] deleteObject:self];
return;
}
// Code to handle if it is a real value
我遇到的问题是,每次以这种方式删除一行时,它都会向 undoManager 注册。 因此,如果我编辑一行,按 Return 键(这将创建一个新行),然后单击结束编辑,该行就会消失。 但是,如果我随后撤消,空字段会再次出现。 我的目标是让 undoManager 忽略涉及空字段的删除操作。
我该怎么办呢? 我尝试在多个位置(例如 -didTurnIntoFault
)使用 [[[self ManagedObjectContext] undoManager]disableUndoRegistration]
和关联的 enableUndoRegistration
,但是我怀疑撤消注册可能会在该方法之前发生)
In my Core Data model, I've got a relationship called listItems
which links to several listItem
entities, each with a stringValue
attribute. I've created a control which is essentially a list of NSTextFields
, one for each list item. The control is bound to listItems
properly, and I've set it up so that pressing the return key creates a new field directly under the currently-edited one and changes the focus to the new field. So, essentially, to add a new item, the user presses Return.
Likewise, if the user ends editing and the currently-edited field is empty, the field is removed (as in, empty fields only appear during "edit mode", so to speak). This works pretty well. Basically, in my listItem
NSManagedObject subclass, I do the following:
// Don't allow nil values
if (!value && [[self.recipe ingredients] count] > 1) {
for (EAIngredientRef *ingredient in [self.recipe ingredients]) {
if ([[ingredient sortIndex] integerValue] > [[self sortIndex] integerValue]) {
[ingredient setSortIndex:[NSNumber numberWithInteger:([[ingredient sortIndex] integerValue]-1)]];
}
}
[[self managedObjectContext] deleteObject:self];
return;
}
// Code to handle if it is a real value
The problem I am encountering is that each time a row is deleted this way, it registers with the undoManager. Thus, if I edit a row, press Return (which creates a new row), and click away to end editing, the row disappears. However, if I then undo, the empty field reappears. My goal is to have delete operations involving empty fields be ignored by the undoManager.
How would I go about this? I've tried using [[[self managedObjectContext] undoManager] disableUndoRegistration]
and the associated enableUndoRegistration
in several spots (such as -didTurnIntoFault
, but I suspect that the undo registration might be happening prior to that method)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您更深入地研究核心数据文档,您会发现隐藏了这个花絮:
通常直到事件循环结束时才会向撤消管理器注册更改,因此在之后注册您已重新打开撤消注册。 上面的内容强制它在您需要时发生。
If you dive more deeply into the Core Data docs, you'll find this tidbit hidden away:
Changes are not registered with the undo manager normally until the end of the event loop, and so were being registered after you'd turned undo registration back on. The above forces it to occur when you want.