How to make a deep copy of NSManagedObject in master data

I am trying to duplicate an existing NSManagedObject and its associated sub-objects in Core Data. I cannot find an easy way to do this.

I have an NSArrayController that populates from a Core Data database. I want to take an object in selectionIndex and make a deep copy, keeping it connected to the same parent and copying all the child objects.

Any help is appreciated!

Thanks to TechZen for the link. I used a sample code from this site and used this call code:

RuleSetVersion *object = [[ruleSetVersionArrayController selectedObjects] lastObject];

NSString *parentEntity = @"RuleSet";

RuleSetVersion *newObject = (RuleSetVersion*)[self copyObject:object toContext:[self managedObjectContext] parent:parentEntity];

[newObject setRuleSetEffectiveDate:[[NSDate alloc] init]];
[newObject setRuleSetVersionLastModifiedDate:[[NSDate alloc] init]];

[newObject setRuleSet:object.ruleSet];

NSError *error;

if ([managedObjectContext save:&error] == NO) {
    [NSApp presentError:error];
}
+5
source share
1 answer
+1

All Articles