Search code examples
ioscore-datansmanagedobjectcontextrollback

iOS Coredata rollback is not working


In my app I'm following CoreDataBooks for implementing Core data. I have a list of items, when user selects any item, a detail screen comes up with edit option. The details screen is having instance of ManagedObjectContext which is child of ManagedObjectContext instance in list. Now when I edits something on details screen I'm updating values in entity taken from child MOC instance. On cancel when I call rollback child MOC, It does nothing. Not sure why rollback is not restoring values to previous one which were last saved.

- (void)cancelAction:(id)sender
{
    _isEditing = NO;
    [_managedObjectContext rollback];
    [self configureNavBar];
    [table reloadData];
}

Solution

  • Can you check if your UndoManager is set in your MOC, that is if you are using UndoManager.

    Read this response that may help on that end: undo all changes made in the child view controller

    But if you are following CoreDataBooks example you can see the Method

    (void)addViewController:(AddViewController *)controller didFinishWithSave:(BOOL)save
    

    that shows that the Child view controller will save its MOC only when the flag save is set, otherwise it will call save on the parent's view controller thus rolling back to the data stored in the Parent's ManagedObject data.