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 beginUndoGrouping
and then aNSUndoManager endUndoGrouping
followed by aNSUndoManager undo
.That is the correct way to roll back changes. The
NSManagedObjectContext
has its own internalNSUndoManager
that you can access.Update showing example
Because the
NSUndoManager
is 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@dynamic
accessors then you will be fine.Try
[managedObjectContext refreshObject:ingredient mergeChanges:NO]
prior to the secondNSLog
call.As per Apple's documentation
Using
Removes everything from the undo stack, discards all insertions and deletions, and restores updated objects to their last committed values.
Here