问题描述
我想在Core Data中复制现有NSManagedObject和相关子对象。我似乎找不到一个简单的方法来做到这一点。
我有一个从Core Data数据库填充的NSArrayController。我想在selectionIndex取对象,并做一个深层副本,保持它相同的父对象和复制所有子对象。
任何帮助是赞赏! / p>
感谢TechZen的链接。我使用该网站的示例代码并使用这个调用代码:
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];
}
查看此答案及其连结的范例程式码:
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) {
[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?
这篇关于如何在Core Data中创建NSManagedObject的深层副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!