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.