CoreData saves / returns unsaved changes

I have a list of objects in UITableView, managed NSFetchedResultsController. When the user selects an object, an edit view is displayed allowing the user to edit the selected instance NSManagedObject. The properties of the object are updated in the object as the user makes changes.

The fact is that, as usual, the "Save" button and the "Cancel" button. If the user deletes the saved data, it is saved NSManagedObjectContext, and this change is reflected. However, if the user cancels the rollback, I need the object to return to the state it was in before. The call is [managedObjectContext rollback]not suitable for this purpose.

Does anyone know how to implement this correctly? I cannot store object properties as temporary separate ivars during editing, because there are several objects that can be edited, all of which have different properties.

Update

I am currently saving NSManagedObjectto an instance variable and calling save:to save and rollbackcancel. Instance variables are modified with object.property = somethingor [object setValue:something forKey:@"property"]. This does not work properly, instead, this behavior is created:

  • If you edit and save, the changes are updated as expected. However, if you edit and cancel again without making any changes, the state of the object will revert back as it was before saving.

  • , , . ( ) , .

:

- , NSFetchedResultsController, ? :

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSString *entityName = self.entityName;
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

[fetchRequest setIncludesPendingChanges:NO];

NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
+5
4

CoreData, ( ), on .

+2

, NSUndoManager , iOS. : fooobar.com/questions/424351/...

+3

NSManagedObject . .

0

All Articles