Core Data deleteObject: sets attributes to nil

允我心安 提交于 2020-01-14 13:32:07

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/2968063/core-data-deleteobject-sets-attributes-to-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!