Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo core data managed object

I have this code:

Store* store = [NSEntityDescription insertNewObjectForEntityForName:@"Store"];
store.name = @"My Company"
...

Now the store is managed in the context and will be saved when the context is saved, but I have a button where the user can cancel the form where data is collected. How do I undo or remove this from the context? Or am I thinking wrong?

like image 281
LuckyLuke Avatar asked May 24 '12 20:05

LuckyLuke


People also ask

What is managed object Core Data?

Core Data uses a schema called a managed object model — an instance of NSManagedObjectModel . In general, the richer the model, the better Core Data is able to support your application. A managed object model allows Core Data to map from records in a persistent store to managed objects that you use in your application.

What is the purpose of managed object context NSManagedObjectContext in Objective C?

A managed object context is an instance of NSManagedObjectContext . Its primary responsibility is to manage a collection of managed objects. These managed objects represent an internally consistent view of one or more persistent stores.

What is Nsfetchrequest?

A description of search criteria used to retrieve data from a persistent store.


1 Answers

Core Data has built-in support for undo, so you can undo individual changes by sending the -undo message to the context:

[store.managedObjectContext undo];

It also supports -redo. You can undo all changes up to the most recent save using the -rollback method:

[store.managedObjectContext rollback]

as indicated in @melsam's answer.

like image 121
Caleb Avatar answered Oct 25 '22 23:10

Caleb