1

I have an NSManagedObject with attributes that a user can edit with a view. The view is populated with the values from the object, the user can edit the values, and the values are written back to the object. I want the user to explicitly tap a save or cancel button to commit or undo the changes.

The problem is that the view is in a UITabbarController where other things are going on. The user might perform operations in another tab where [NSManagedObjectContext save] or [NSManagedObjectContext undo] might get called. This would affect the NSManagedObject (from the first mentioned tab) before the user decides if they want to save or cancel it.

Is there a way around this? Can we temporarily disable persistence on an NSManagedObject until the user taps the button?

chris
  • 16,324
  • 9
  • 37
  • 40
  • hello Chris, you mind posting an example code that solved this issue? You mentioned below that you used cloning the original object. I'm facing the same issue and was thinking of cloning as well instead of using NSMutableDictionary. – Joo Park Apr 17 '10 at 18:43

1 Answers1

1

There is no way to disable persistence for a managed object. Instead I would recommend an approach like this:

Typically, when showing a view that edits a particular object, you update that view with the data from the object in the viewWillAppear: method and update the object with the changed data in a corresponding "save" action or viewWillDisappear:.

gerry3
  • 21,420
  • 9
  • 66
  • 74
  • 1
    If you want the user to explicitly "save" by hitting a save button then implement only the first half of gerry3's approach. When this view appears, update its contents according to the updated object and only save the changes when the user taps "save" (not on `viewWillDiappear`). The only problem you have is that if the user makes any changes, does not save them, goes to another tab where changes are made and then goes back to edit the object, his changes will be lost. But I find that okay since the object has changed since he last made changes. – Dimitris Feb 21 '10 at 10:37
  • I resolved this by cloning the original object, editing that, and using it to replace the original if the user presses save. There were a few more lines to clean-up the temporary cloned objects, but the solution seems to work. Thanks for your help! – chris Feb 22 '10 at 18:39
  • I discuss using temporary managed objects or, even better, `NSMutableDictionary` in this answer: http://stackoverflow.com/questions/2251158/can-i-create-manually-an-instance-of-an-core-data-entity-class-for-temporary-usag/2251183#2251183 – gerry3 Feb 22 '10 at 20:03