Reputation: 7227
I have two entities A and B. A has an one-to-many relationships to B. After I changed some properties in B, how could I undo all the changes in B? I have tried rollback method, but that will affect other properties in A.
Upvotes: 2
Views: 304
Reputation: 3733
Try calling disableUndoRegistration
on the undoManager just before making any change to A, and calling enableUndoRegistration
just after making any change to A.
Now, where to put this code? Ideally you'd override some method/s in NSManagedObject for your A class, but which ones? I don't think the validate
methods would work, at least not for disabling the undo registration, because I think the change may already have been registered with the undoManager by the time they are called. It's tempting to use will/didChangeValueForKey:
-- but the class reference says "You must not override this method."
So I think you're stuck addressing this in any interface action that can affect A.
Edit -- Here's the sample you requested:
- (IBAction) someAction:(id)sender {
BOOL isA = NO;
NSManagedObjectContext currentContext = nil;
if ([[sender objectControlledBySender] isKindOfClass:[subclassedManagedObjectA class]]) {
isA = YES;
currentContext = // get a ref to your current context, or just make it an unretained property of this controller's class
[currentContext.undoManager disableUndoRegistration];
}
// Make the changes to the object accessed via sender.
if (isA)
[currentContext.undoManager enbleUndoRegistration];
}
The "objectControlledBySender" is necessarily vague, because sender
could by any kind of control, with any kind of accessors to the objects it displays or edits or selects.
Upvotes: 1