How to separate consecutive INSERTs into separate transaction/UNDO/REDO?
I'm trying to find a way on how to separate two INSERTs into entity/table that was done consecutively, to be seen as two separate transaction and should be UNDOne individually, also.
I got a method that does 2 INSERTs into a table/entity consecutively. When an [moc undo] is executed, the 2 inserts are of course UNDOne together. How can I make the 2 INSERTs as separate transactions? I tried the beginUndoGrouping/endUndoGrouping but wasn't able to make it work. Also tried the setGroupsByEvent, I got error when saving the MOC.
Below is a simplified code snippet. Thanks so much for your help!!
//-------------------------------------- METHODS IN CONTROLLER ABC
-(IBAction) undoInsert:(id) sender{
//[moc.UndoManager undo];
[moc undo];
}
-(void) insertNumber:(NSNotification*)notification {
double theNumber = [[…. userInfo] objectForKey:aNumber];
Data *newRow = [NSEntityDescription insertNewObjectForEntityForName:@"DATA" inManagedObjectContext:self.thisMOC];
newRow.VALUE = [NSNumber numberWithDouble:theNumber];
newRow.TYPE = @"A";
//...
if (condition) {
[thisMOC save:&error];
}
}
-(void) insertTotal:(NSNotification*)notification {
//i tried here the beginUndoGrouping/endUndoGrouping to separate this INSERT but it still gets UNDOne together
//[thisMOC setGroupsByEvent:NO];
//[thisMOC.undoManager beginUndoGrouping];
//...
double theNumber = [[…. userInfo] objectForKey:aNumber];
Data *newRow = [NSEntityDescription insertNewObjectForEntityForName:@"DATA" inManagedObjectContext:self.thisMOC];
newRow.VALUE = [NSNumber numberWithDouble:theNumber];
newRow.TYPE = @"T";
//...
//[thisMOC.undoManager endUndoGrouping];
//[thisMOC setGroupsByEvent:YES];
[thisMOC save:&error];
}
//-------------------------------------- end of METHODS IN CONTROLLER ABC
//--------------------------------------- METHODS IN CONTROLLER XYZ
//Note: the Controllers ABC and XYZ are totally independent/unaware of each other, XYZ passes data only thru Notification.
-(IBAction) doInsertNumber:(id) sender {
//aNumber is given a value somewhere, is passed as user info dictionary numberDict
[[NSNotificationCenter defaultCenter]
postNotificationName:@"InsertNumber" object:self userInfo:numberDict];
//..
}
-(IBAction) doInsertTotal:(id) sender {
//these two INSERTs gets UNDOne together, I need to separate the UNDO for these 2 numbers
//aNumber, xNumber are given values somewhere, packaged into dictionary numberDict
[[NSNotificationCenter defaultCenter]
postNotificationName:@"InsertNumber" object:self userInfo:numberDict];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"InsertTotal" object:self userInfo:numberDict];
}
//-------------------------------------- end of METHODS IN CONTROLLER XYZ
|