开发者

How to make Deep Copy of NSManagedObject in Core Data

开发者 https://www.devze.com 2023-01-18 04:03 出处:网络
I am trying to make a duplicate of an existing NSManagedObject and related sub-objects in Core Data.I can\'t seem to find an easy way to do this.

I am trying to make a duplicate of an existing NSManagedObject and related sub-objects in Core Data. I can't seem to find an easy way to do this.

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

Any assistance is appreciated!

Thanks to TechZen for the link. I used the sample code from that site and used this calling 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) {
开发者_JAVA技巧    [NSApp presentError:error];
}


It's fairly involved. See this answer and the sample code linked from it:

How do I copy or move an NSManagedObject from one context to another?

0

精彩评论

暂无评论...
验证码 换一张
取 消