Test set up:
// Create a clean context for this test.
NSManagedObjectContext *cleanContext = [MyContextManager sharedInstance] newMOC];
// Create a core data object.
MyCoreDataObject *myObject = [MyCoreDataObject insertNewObjectInMoc:cleanContext];
myObject.name = @"Test Name 1";
// Create an undo manager - set on the context.
cleanContext.undoManager = [NSUndoManager new];
// Turn off automatic grouping
cleanContext.undoManager.groupsByEvent = NO;
[cleanContext.undoManager beginUndoGrouping];
myObject.name = @"Test Name 2";
XCTAssertEqualObjects(myObject.name, @"Test Name 2", @"Setting name failed");
// End undo grouping
[cleanContext.undoManager endUndoGrouping];
// Undo the group that has just been created
[cleanContext.undoManager undoNestedGroup];
XCTAssertEqualObjects(myObject.name, @"Test Name 1", @"Undo failed");
I see that the undo manager has a private stack of invocations I can access via [cleanContext.undoManager performSelector:@selector(_undoStack)]
When looking at it even though I've turned off group by events there are new undo groupings that I haven't added.
This causes a lot of pain - errors such as NSUndoManager is in invalid state
occur even though I correctly manage beginning and ending undo groups.
Does setting attributes on a core data object automatically add them irrespective of the group by events settings?
Has anyone managed to create a stable undo functionality using the built in undo manager?
Aucun commentaire:
Enregistrer un commentaire