I have an object, I make some changes to it, but I don’t want to save them, I want the ‘old’ values.
I’ve tried with:
[managedObjectContext rollback];
[managedObjectContext redo];
[managedObjectContext reset];
and none of them seems to work …
NSLog(@"current: %@",ingredient.name); // ===> bread
[ingredient setName:@"test new data"];
NSLog(@"new: %@",ingredient.name); // ===> test new data
[managedObjectContext rollback];
[managedObjectContext redo];
[managedObjectContext reset];
NSLog(@"current: %@",ingredient.name); // ===> test new data
// I want again ===> bread
Should I refetch the object again ?
thanks,
r.
Wrap your changes in a
NSUndoManager beginUndoGroupingand then aNSUndoManager endUndoGroupingfollowed by aNSUndoManager undo.That is the correct way to roll back changes. The
NSManagedObjectContexthas its own internalNSUndoManagerthat you can access.Update showing example
Because the
NSUndoManageris nil by default on Cocoa Touch, you have to create one and set it into theNSManagedObjectContext first.Also make sure that your accessors are following the rules of KVO and posting
-willChange:,-didChange:,-willAccess:and-DidAccess:notifications. If you are just using@dynamicaccessors then you will be fine.