views:

210

answers:

2

In a Core Data project, I've got two buttons on my interface (a table), one which edits the data about a person and saves it to the db, and one lets you visually fiddle around with the data without actually saving it to the db. How do I 'detach' the core data object, so that it so it won't write data to the db? I've tried to write a new NSObject class which mimic the NSManagedObject class, and then move the data into the NSObject class, but it still changes the db!

//button for editing and saving
PersonObj *person = (PersonObj *)[fetchedResultsController objectAtIndexPath:indexPath];
PersonEditViewController *editViewController = [[PersonEditViewController alloc] initWithStyle:UITableViewStyleGrouped];
// puts the managed object into the new view controller
editViewController.person = person;
[self.viewController pushViewController:editViewController animated:animated];
[editViewController release];

//button for editing and NOT saving
PersonObj *person = (PersonObj *)[fetchedResultsController objectAtIndexPath:indexPath];
PersonFiddlingViewController *fiddling = [[PersonFiddlingViewController alloc] initWithStyle:UITableViewStyleGrouped];
// move db data into non-db class??? not working
fiddling.eyeColor = person.eyeColor;
fiddling.name = person.name;
[self.viewController pushViewController:fiddling animated:animated];
[fiddling release];
A: 

I've only been using Core Data for a few days now (just converted a project to use it for storing user data), but what you describe doesn't make sense. How can the backing store (db) be saving changes unless you're committing the changes using NSManagedObjectContext:save: somewhere? Can you post your code where you're calling that method? Perhaps you're mistakenly doing it from some other context (maybe viewDidLoad: or something).

Shaggy Frog
+1  A: 

I don't think you understand how CD works. You can't detach an object from CoreData, CD is the backing store for the object.

I am also pretty certain that the issues you are seeing are not related to the code you have included above, because even in the first case there is nothing in the code you have that will commit the changes to persistent storage. Absent a call to NSManagedObjectContext:save: all changes to your object graph will be transient.

In other words, in order to do what you want to do (tweak stuff without saving it), just don't call save: on the context the object is in after you tweak it. If you need to save other changes you can either explicitly roll back the tweaks before the save, or you can wrap the tweaked objects in their own context before fiddling with them:

NSManagedObjectContext *tweakingContext = [[NSManagedObjectContext alloc] init];
tweakingContext.persistentStoreCoordinator = person.managedObjectContext.persistentStoreCoordinator;
PersonObj *tweakablePerson = [tweakingContext objectWithID:person.managedObjectID];

//Do your stuff

[tweakingContext release];

The above code will give you you new object (tweakablePerson) that is faulted in from the same data as person, but in an independent context you will never save. If you do ever want to save it you can, but you will have to deal with other issues (potential save conflicts if other saves have occurred).

Again, while that answers the question you are asking, I don't think it will solve your problem because the issue is almost certainly not in the fetch or object creation code (which you show), but in the save code which you have not listed.

Louis Gerbarg