I'm instantiating a NSManagedObjectContext
object at the Application delegate level and sharing it across all my UIViewController
s. Here's the code that I use to access it in one of my View Controllers:
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
modelObj = (Model *) [NSEntityDescription insertNewObjectForEntityForName:@"Model" inManagedObjectContext:[appDelegate managedObjectContext]];
Now in this screen, I have a UITableView
with 9 rows & each cell has a UITextField
. As the user inputs values into the textfields, I assign them into modelObj
. Now, my user has an option to cancel out and discard all the changes or save them to disk. I have the save code working fine. But in the case when a user tries to discard the changes, I'm not sure what to do. There doesn't seem to be a [managedObjectContext discardChanges]
method to throw them all away.
I can think of a couple of ways of solving this.
- Create a new instance of
NSManagedObjectContext
for each controller instead of sharing one across the application. - Or, I could create a bunch of
NSString
s in my code and save user values in them and callinsertNewObjectForEntityForName:
only if the user clicks save.
Which way is the right one? Or is there a way to make NSManagedObjectConext
discard all the changes that were made to it?
Thanks,
Teja.