I have an image made up of multiple layers (managed object), each represented by a view. When I clear these layers, I delete them from front to back, expecting the undo to restore them in opposite order, back to front, therefore creating their views from back to front as well, giving me the correct order. However, the order in which the layers (managed object) are restored is seemingly random. Is there a way to ensure that the layers are re-inserted in the opposite order they were deleted (since undo is usually a stack, this is what i would expect).
Note that all the layer deletions are happening within a single undo grouping, so its not like I'm hitting the undo button multiple times, it's all within one undo call.
[self.managedObjectContext.undoManager beginUndoGrouping];
//remove all layers but the bg
NSMutableOrderedSet * layers = self.interactionView.currentSketchPage.layersSet;
for (int i = layers.count-1; i >= 1; i--) {
[self.managedObjectContext deleteObject:[layers objectAtIndex:i]];
}
[self.managedObjectContext.undoManager endUndoGrouping];
Note that deleting some of these layers can cause other objects to be deleted from a cascade, so maybe that is affecting it too?
Edit: wanted to note the switching the order of the for loop does not change the order the layers are restored.