核心数据deleteObject:将属性设置为nil

发布于 2024-09-04 02:40:55 字数 1762 浏览 3 评论 0原文

我正在我的应用程序中实现撤消/重做机制。这对于很多情况都适用。但是,我无法撤消过去的deleteObject:。该对象已正确保存在撤消队列中,并且在调用撤消时我将其取回并重新插入到核心数据堆栈中。问题是,当我删除它时,它的所有属性都被设置为 nil。

我有一个实体“Canvas”,与“Graphic”实体之间存在称为“graphics”的一对多关系,该实体的逆设置为“canvas”。删除图形然后将其插入回去,不起作用。这是代码(重做方法基本相同):

- (void)deleteGraphic:(id)aGraphic {
 //NSLog(@"undo drawing");
 //Prepare the undo/redo
 [self.undoManager beginUndoGrouping];
 [self.undoManager setActionName:@"Delete Graphic"];

 [[self.detailItem valueForKey:@"graphics"] removeObject:aGraphic];
 [[self managedObjectContext] deleteObject:aGraphic];

 //End undo/redo
 [self.undoManager registerUndoWithTarget:self selector:@selector(insertGraphic:) object:aGraphic];
 [self.undoManager endUndoGrouping];

 NSLog(@"graphics are %@", [self sortedGraphics]);

 //Update drawing
 [self.quartzView setNeedsDisplay];
}

奇怪的是:

删除之前:

graphics are (
<NSManagedObject: 0x1cc3f0> (entity: Graphic; id: 0x1c05f0 <x-coredata:///Graphic/t840FE8AD-F2E7-4214-822F-7994FF93D4754> ; data: {
canvas = 0x162b70 <x-coredata://A919979E-75AD-474D-9561-E0E8F3388718/Canvas/p20>;
content = <62706c69 73743030 d4010203 04050609 0a582476 65727369 6f6e5424 746f7059 24617263 68697665 7258246f 626a6563 7473>;
frameRect = nil;
label = nil;
order = 1;
path = "(...not nil..)";
traits = "(...not nil..)";
type = Path;
})

重做之后:

graphics are (
<NSManagedObject: 0x1cc3f0> (entity: Graphic; id: 0x1c05f0 <x-coredata:///Graphic/t840FE8AD-F2E7-4214-822F-7994FF93D4754> ; data: {
canvas = nil;
content = nil;
frameRect = nil;
label = nil;
order = 0;
path = nil;
traits = nil;
type = nil;
}),

你可以看到它是同一个对象,只是被 Core Data 完全漂白了。关系删除规则显然与此无关,因为我在测试中将它们设置为“无操作”。

I am implementing an undo/redo mechanism in my app. This works fine for lots of cases. However, I can't undo past deleteObject:. the object is correctly saved in the undo queue, and I get it back and reinsterted into the Core Data stack just fine when calling undo. The problem is that all it's attributes are getting set to nil when I delete it.

I have an entity "Canvas" with a to-many relationship called "graphics" to a "Graphic" entity, which has its inverse set to "canvas". Deleting a Graphic, then inserting it back, doesn't work. Here's the code (the redo method is basically the same):

- (void)deleteGraphic:(id)aGraphic {
 //NSLog(@"undo drawing");
 //Prepare the undo/redo
 [self.undoManager beginUndoGrouping];
 [self.undoManager setActionName:@"Delete Graphic"];

 [[self.detailItem valueForKey:@"graphics"] removeObject:aGraphic];
 [[self managedObjectContext] deleteObject:aGraphic];

 //End undo/redo
 [self.undoManager registerUndoWithTarget:self selector:@selector(insertGraphic:) object:aGraphic];
 [self.undoManager endUndoGrouping];

 NSLog(@"graphics are %@", [self sortedGraphics]);

 //Update drawing
 [self.quartzView setNeedsDisplay];
}

and here's the wierdness:

Before delete:

graphics are (
<NSManagedObject: 0x1cc3f0> (entity: Graphic; id: 0x1c05f0 <x-coredata:///Graphic/t840FE8AD-F2E7-4214-822F-7994FF93D4754> ; data: {
canvas = 0x162b70 <x-coredata://A919979E-75AD-474D-9561-E0E8F3388718/Canvas/p20>;
content = <62706c69 73743030 d4010203 04050609 0a582476 65727369 6f6e5424 746f7059 24617263 68697665 7258246f 626a6563 7473>;
frameRect = nil;
label = nil;
order = 1;
path = "(...not nil..)";
traits = "(...not nil..)";
type = Path;
})

After redo:

graphics are (
<NSManagedObject: 0x1cc3f0> (entity: Graphic; id: 0x1c05f0 <x-coredata:///Graphic/t840FE8AD-F2E7-4214-822F-7994FF93D4754> ; data: {
canvas = nil;
content = nil;
frameRect = nil;
label = nil;
order = 0;
path = nil;
traits = nil;
type = nil;
}),

You can see it's the same object, just totally bleached by Core Data. The relationship delete rouls apparently have nothing to do with it as I've set them to "No Action" in a test.

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

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

发布评论

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

评论(1

罪#恶を代价 2024-09-11 02:40:55

好吧,我对修复并不满意,但一种方法是在删除对象之前在上下文上执行 [moc save]。不幸的是,在撤消/重做情况下,这意味着我必须在每次操作后进行保存,这是次优的,但可以解决此问题。

Well, I'm not happy with the fix but one way is to perform a [moc save] on the context prior to deleting the object. Unfortunately in undo/redo situations this means that I have to save after every operation, which is suboptimal but fixes this problem.

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