核心数据DeleteObject的:设置属性使其为零 [英] Core Data deleteObject: sets attributes to nil

查看:171
本文介绍了核心数据DeleteObject的:设置属性使其为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现我的应用程序的撤销/恢复机制。这对很多情况下正常工作。但是,我不能撤消过去DeleteObject的:.对象被正确地保存在撤销队列中,我拿回来,并reinsterted到核心数据调用时撤消堆栈就好了。问题是,它的所有的属性越来越设为零,当我将其删除。

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.

我有一个实体画布与所谓的图形一个一对多的关系,一个图形的实体,它有逆设置为画布上。删除图形,然后将其插入回来,不能正常工作。这里的code(重做方法基本相同):

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];
}

和这里的wierdness:

and here's the wierdness:

删除之前:

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;
}),

您可以看到它的同一个对象,只是完全由核心数据漂白。关系删除rouls显然有什么关系呢,因为我已经将它们设置为在测试不采取行动。

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.

推荐答案

好吧,我不开心的修复,但一种方法是之前删除的对象上执行的背景下[MOC保存。不幸的是撤销/重做的情况下,这意味着我有充分的操作,这是最理想的,但修复了这个问题后保存。

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.

这篇关于核心数据DeleteObject的:设置属性使其为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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