On 2011 Jan 21, at 16:03, Kenneth Baxter wrote:

> - (MyNode *)createChild {
> MyNode *child = [NSEntityDescription insertNewObjectForEntityForName:…

Core Data undo will take care of that automatically.

> // Set a bunch of properties on the new child.

Core Data undo will take care of that automatically.

> [[self mutableSetValueForKey:@"children"] addObject:child];

Core Data undo will take care of that automatically.

> // Set a bunch more properties on the new child and other core data managed 
> objects

Core Data undo will take care of that automatically as long as the other 
objects are in the same managed object context.  If they're not, think about if 
you really need multiple mocs.

> // Let the front end of the application know about it
> [[NSNotificationCenter defaultCenter] 
> postNotificationName:@"ChildNodeAddedNotification"…

Why?  As Dave Fernandes suggested, use bindings.  If you're using custom views, 
you have the choice of either adding bindings support so you can take advantage 
of Core Data's undo, or writing your own undo code, trying to slip your undo 
tasks into the undo stack without upsetting Core Data.  The former can take 
significant code but is straightforward.  The latter can be extremely dangerous 
to your mental health and those around you.

Now, regarding the inverse operation,

> - (void)deleteChild:(MyNode *)aChild {

you can pretty much read my comments above backwards.  One additional trick: 
Core Data will automatically (and undoably) delete child objects for you if use 
a Cascade Delete Rule in the relationship from parent to child.  This code…

> NSArray *affectedChildren = [aChild selfAndAllDescendants] …

is not necessary.  Rip that and subsequent code out of there.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to