Is it possible to undo all changes made in a view controller without affecting the changes made in its parent view controller? btw. They are with the same managedObjectContext. ps. I have two entities A and B. A has an to-many relationships to B. View controller VCA is used to perform some changes in A. View controller VCB is used to perform some changes in B. VCA is the parent view controller of VCB.
Asked
Active
Viewed 185 times
0
-
Can you clarify what sort of changes you're talking about? What data are you changing, and where does it live? – strings42 May 01 '12 at 18:27
2 Answers
1
You should create new NSManagedObjectContext for child view controller and attach undo to it. NSManagedObjectContext is very light and doesn't cost very much.

thom_ek
- 673
- 4
- 7
-
yes, it's a solution, but then I have to copy the manage obj from one contex to the created one. – lu yuan May 02 '12 at 10:57
-
Not copy, but pass objectId of it. I think that there is no other solution for You. – thom_ek May 02 '12 at 13:49
-
When i add the child object B to the parent object A, it doesn't work because they are not in the same context. Should I do copy the changes in B context into A context first? Any other solution for this problem? – lu yuan May 03 '12 at 05:24
1
No need to create a new context. When you leave the child view controller, you have the option of either
[managedObjectContext save:&error];
or
[managedObjectContext rollback];
In the latter case, all your new entity instances, data modifications etc. will be discarded.

Mundi
- 79,884
- 17
- 117
- 140
-
Rollback would undo all changes in the context. When the parent and children view controller in the same context, it could not make out. – lu yuan May 02 '12 at 11:00
-
No. It rolls back all changes **since the last save**. All you have to do is to save before launching the child view controller. – Mundi May 02 '12 at 13:45
-
But when I get back to the parent view controller, the saved changes couldn't be reversed. – lu yuan May 02 '12 at 14:16
-
Yes, only the saved changes in the parent view controller. If you want those to be reversed, simply don't save before. – Mundi May 02 '12 at 19:22
-
Could I group the changes in the child view controller and undo all changes in the group? – lu yuan May 02 '12 at 20:53
-
So your parent view controller has another parent controller to which it could return without saving? In this case simply reverse the changes of the top child view controller programmatically. Normally it is just creating and configuring a new entity instance and then deleting it. – Mundi May 03 '12 at 10:08
-
After creating a new MOC for the child view controller (B), When i add the child object B to the parent object A, it doesn't work because they are not in the same context. – lu yuan May 03 '12 at 10:17
-
-
http://stackoverflow.com/questions/4853475/nested-undo-group-with-coredata i wish the undo grouping could help me out, but until now i could not handle with it. – lu yuan May 03 '12 at 10:38