我有一个托管对象(“A”),它包含各种属性和关系类型,它的关系也有自己的属性&关系。我想做的是“复制”或“复制”以对象“A”为根的整个对象图,从而创建一个与“A”非常相似的新对象“B”。
更具体地说,“B”(或其子代)所包含的关系都不应指向与“A”相关的对象。应该有一个完全相同的关系完整的新对象图,所有对象具有相同的属性,但当然不同的id。
有一种明显的手动方式可以做到这一点,但我希望了解一种更简单的方法,这在核心数据文档中并不完全明显。
TIA!
答案 0 :(得分:58)
这是我创建的一个类,用于执行托管对象的“深层复制”:属性和关系。请注意,这不会检查对象图中的循环。 (感谢Jaanus的起点......)
@interface ManagedObjectCloner : NSObject {
}
+(NSManagedObject *)clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context;
@end
@implementation ManagedObjectCloner
+(NSManagedObject *) clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context{
NSString *entityName = [[source entity] name];
//create new object in data store
NSManagedObject *cloned = [NSEntityDescription
insertNewObjectForEntityForName:entityName
inManagedObjectContext:context];
//loop through all attributes and assign then to the clone
NSDictionary *attributes = [[NSEntityDescription
entityForName:entityName
inManagedObjectContext:context] attributesByName];
for (NSString *attr in attributes) {
[cloned setValue:[source valueForKey:attr] forKey:attr];
}
//Loop through all relationships, and clone them.
NSDictionary *relationships = [[NSEntityDescription
entityForName:entityName
inManagedObjectContext:context] relationshipsByName];
for (NSRelationshipDescription *rel in relationships){
NSString *keyName = [NSString stringWithFormat:@"%@",rel];
//get a set of all objects in the relationship
NSMutableSet *sourceSet = [source mutableSetValueForKey:keyName];
NSMutableSet *clonedSet = [cloned mutableSetValueForKey:keyName];
NSEnumerator *e = [sourceSet objectEnumerator];
NSManagedObject *relatedObject;
while ( relatedObject = [e nextObject]){
//Clone it, and add clone to set
NSManagedObject *clonedRelatedObject = [ManagedObjectCloner clone:relatedObject
inContext:context];
[clonedSet addObject:clonedRelatedObject];
}
}
return cloned;
}
@end
答案 1 :(得分:42)
这些答案让我非常接近,虽然他们似乎确实有一些缺点:
1,我接受了Z S的建议并将其作为NSManagedObject的一个类别,这对我来说似乎有点清洁。
第二,我的对象图包含一对一的关系,所以我从levous的例子开始,但请注意,levous的例子不是在一对一关系的情况下克隆对象。这将导致崩溃(尝试从不同上下文中的一个上下文保存NSMO)。我在下面的例子中解决了这个问题。
第三,我提供了已经克隆的对象的缓存,这可以防止对象被克隆两次,因此在新的对象图中重复,并且还可以防止循环。
第四,我添加了一个黑名单(不克隆的实体类型列表)。我这样做的部分原因是为了解决我最终解决方案的一个缺点,我将在下面介绍。注意:如果您使用我理解的CoreData最佳实践,始终提供反向关系,那么这可能会克隆与您要克隆的对象有关系的所有对象。如果您正在使用反转并且您有一个知道所有其他对象的单个根对象,那么您可能会克隆整个对象。我的解决方案是添加黑名单并传入我知道是我想要克隆的其中一个对象的父类的实体类型。这似乎对我有用。 :)
快乐的克隆!
// NSManagedObject+Clone.h
#import <CoreData/CoreData.h>
@interface NSManagedObject (Clone)
- (NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context exludeEntities:(NSArray *)namesOfEntitiesToExclude;
@end
// NSManagedObject+Clone.m
#import "NSManagedObject+Clone.h"
@implementation NSManagedObject (Clone)
- (NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context withCopiedCache:(NSMutableDictionary *)alreadyCopied exludeEntities:(NSArray *)namesOfEntitiesToExclude {
NSString *entityName = [[self entity] name];
if ([namesOfEntitiesToExclude containsObject:entityName]) {
return nil;
}
NSManagedObject *cloned = [alreadyCopied objectForKey:[self objectID]];
if (cloned != nil) {
return cloned;
}
//create new object in data store
cloned = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
[alreadyCopied setObject:cloned forKey:[self objectID]];
//loop through all attributes and assign then to the clone
NSDictionary *attributes = [[NSEntityDescription entityForName:entityName inManagedObjectContext:context] attributesByName];
for (NSString *attr in attributes) {
[cloned setValue:[self valueForKey:attr] forKey:attr];
}
//Loop through all relationships, and clone them.
NSDictionary *relationships = [[NSEntityDescription entityForName:entityName inManagedObjectContext:context] relationshipsByName];
for (NSString *relName in [relationships allKeys]){
NSRelationshipDescription *rel = [relationships objectForKey:relName];
NSString *keyName = rel.name;
if ([rel isToMany]) {
//get a set of all objects in the relationship
NSMutableSet *sourceSet = [self mutableSetValueForKey:keyName];
NSMutableSet *clonedSet = [cloned mutableSetValueForKey:keyName];
NSEnumerator *e = [sourceSet objectEnumerator];
NSManagedObject *relatedObject;
while ( relatedObject = [e nextObject]){
//Clone it, and add clone to set
NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context withCopiedCache:alreadyCopied exludeEntities:namesOfEntitiesToExclude];
[clonedSet addObject:clonedRelatedObject];
}
}else {
NSManagedObject *relatedObject = [self valueForKey:keyName];
if (relatedObject != nil) {
NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context withCopiedCache:alreadyCopied exludeEntities:namesOfEntitiesToExclude];
[cloned setValue:clonedRelatedObject forKey:keyName];
}
}
}
return cloned;
}
- (NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context exludeEntities:(NSArray *)namesOfEntitiesToExclude {
return [self cloneInContext:context withCopiedCache:[NSMutableDictionary dictionary] exludeEntities:namesOfEntitiesToExclude];
}
@end
答案 2 :(得分:24)
我已经更新了user353759的答案,以支持一种关系。
@interface ManagedObjectCloner : NSObject {
}
+(NSManagedObject *)clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context;
@end
@implementation ManagedObjectCloner
+(NSManagedObject *) clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context{
NSString *entityName = [[source entity] name];
//create new object in data store
NSManagedObject *cloned = [NSEntityDescription
insertNewObjectForEntityForName:entityName
inManagedObjectContext:context];
//loop through all attributes and assign then to the clone
NSDictionary *attributes = [[NSEntityDescription
entityForName:entityName
inManagedObjectContext:context] attributesByName];
for (NSString *attr in attributes) {
[cloned setValue:[source valueForKey:attr] forKey:attr];
}
//Loop through all relationships, and clone them.
NSDictionary *relationships = [[NSEntityDescription
entityForName:entityName
inManagedObjectContext:context] relationshipsByName];
for (NSString *relName in [relationships allKeys]){
NSRelationshipDescription *rel = [relationships objectForKey:relName];
NSString *keyName = [NSString stringWithFormat:@"%@",rel];
if ([rel isToMany]) {
//get a set of all objects in the relationship
NSMutableSet *sourceSet = [source mutableSetValueForKey:keyName];
NSMutableSet *clonedSet = [cloned mutableSetValueForKey:keyName];
NSEnumerator *e = [sourceSet objectEnumerator];
NSManagedObject *relatedObject;
while ( relatedObject = [e nextObject]){
//Clone it, and add clone to set
NSManagedObject *clonedRelatedObject = [ManagedObjectCloner clone:relatedObject
inContext:context];
[clonedSet addObject:clonedRelatedObject];
}
}else {
[cloned setValue:[source valueForKey:keyName] forKey:keyName];
}
}
return cloned;
}
答案 3 :(得分:15)
这是@Derricks的答案,经过修改以通过查询关系以查看是否已订购来支持新的iOS 6.0订购多人关系。当我在那里时,我在同一个NSManagedObjectContext中为克隆的常见情况添加了一个更简单的-clone方法。
//
// NSManagedObject+Clone.h
// Tone Poet
//
// Created by Mason Kramer on 5/31/13.
// Copyright (c) 2013 Mason Kramer. The contents of this file are available for use by anyone, for any purpose whatsoever.
//
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface NSManagedObject (Clone) {
}
-(NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context withCopiedCache:(NSMutableDictionary *)alreadyCopied exludeEntities:(NSArray *)namesOfEntitiesToExclude;
-(NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context exludeEntities:(NSArray *)namesOfEntitiesToExclude;
-(NSManagedObject *) clone;
@end
//
// NSManagedObject+Clone.m
// Tone Poet
//
// Created by Mason Kramer on 5/31/13.
// Copyright (c) 2013 Mason Kramer. The contents of this file are available for use by anyone, for any purpose whatsoever.
//
#import "NSManagedObject+Clone.h"
@implementation NSManagedObject (Clone)
-(NSManagedObject *) clone {
return [self cloneInContext:[self managedObjectContext] exludeEntities:@[]];
}
- (NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context withCopiedCache:(NSMutableDictionary *)alreadyCopied exludeEntities:(NSArray *)namesOfEntitiesToExclude {
NSString *entityName = [[self entity] name];
if ([namesOfEntitiesToExclude containsObject:entityName]) {
return nil;
}
NSManagedObject *cloned = [alreadyCopied objectForKey:[self objectID]];
if (cloned != nil) {
return cloned;
}
//create new object in data store
cloned = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
[alreadyCopied setObject:cloned forKey:[self objectID]];
//loop through all attributes and assign then to the clone
NSDictionary *attributes = [[NSEntityDescription entityForName:entityName inManagedObjectContext:context] attributesByName];
for (NSString *attr in attributes) {
[cloned setValue:[self valueForKey:attr] forKey:attr];
}
//Loop through all relationships, and clone them.
NSDictionary *relationships = [[NSEntityDescription entityForName:entityName inManagedObjectContext:context] relationshipsByName];
for (NSString *relName in [relationships allKeys]){
NSRelationshipDescription *rel = [relationships objectForKey:relName];
NSString *keyName = rel.name;
if ([rel isToMany]) {
if ([rel isOrdered]) {
NSMutableOrderedSet *sourceSet = [self mutableOrderedSetValueForKey:keyName];
NSMutableOrderedSet *clonedSet = [cloned mutableOrderedSetValueForKey:keyName];
NSEnumerator *e = [sourceSet objectEnumerator];
NSManagedObject *relatedObject;
while ( relatedObject = [e nextObject]){
//Clone it, and add clone to set
NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context withCopiedCache:alreadyCopied exludeEntities:namesOfEntitiesToExclude];
[clonedSet addObject:clonedRelatedObject];
[clonedSet addObject:clonedRelatedObject];
}
}
else {
NSMutableSet *sourceSet = [self mutableSetValueForKey:keyName];
NSMutableSet *clonedSet = [cloned mutableSetValueForKey:keyName];
NSEnumerator *e = [sourceSet objectEnumerator];
NSManagedObject *relatedObject;
while ( relatedObject = [e nextObject]){
//Clone it, and add clone to set
NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context withCopiedCache:alreadyCopied exludeEntities:namesOfEntitiesToExclude];
[clonedSet addObject:clonedRelatedObject];
}
}
}
else {
NSManagedObject *relatedObject = [self valueForKey:keyName];
if (relatedObject != nil) {
NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context withCopiedCache:alreadyCopied exludeEntities:namesOfEntitiesToExclude];
[cloned setValue:clonedRelatedObject forKey:keyName];
}
}
}
return cloned;
}
-(NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context exludeEntities:(NSArray *)namesOfEntitiesToExclude {
return [self cloneInContext:context withCopiedCache:[NSMutableDictionary dictionary] exludeEntities:namesOfEntitiesToExclude];
}
@end
答案 4 :(得分:8)
我注意到目前的答案有几个错误。首先,在迭代过程中似乎正在改变to-Many相关对象的集合。其次,我不确定API中是否有任何变化,但使用NSRelationshipDescription
的String表示作为关键是在抓取那些相关对象时抛出异常。
我做了一些调整,做了一些基本的测试,似乎工作。如果有人想进一步调查那将会很棒!
@implementation NSManagedObjectContext (DeepCopy)
-(NSManagedObject *) clone:(NSManagedObject *)source{
NSString *entityName = [[source entity] name];
//create new object in data store
NSManagedObject *cloned = [NSEntityDescription
insertNewObjectForEntityForName:entityName
inManagedObjectContext:self];
//loop through all attributes and assign then to the clone
NSDictionary *attributes = [[NSEntityDescription
entityForName:entityName
inManagedObjectContext:self] attributesByName];
for (NSString *attr in attributes) {
[cloned setValue:[source valueForKey:attr] forKey:attr];
}
//Loop through all relationships, and clone them.
NSDictionary *relationships = [[NSEntityDescription
entityForName:entityName
inManagedObjectContext:self] relationshipsByName];
for (NSString *relName in [relationships allKeys]){
NSRelationshipDescription *rel = [relationships objectForKey:relName];
if ([rel isToMany]) {
//get a set of all objects in the relationship
NSArray *sourceArray = [[source mutableSetValueForKey:relName] allObjects];
NSMutableSet *clonedSet = [cloned mutableSetValueForKey:relName];
for(NSManagedObject *relatedObject in sourceArray) {
NSManagedObject *clonedRelatedObject = [self clone:relatedObject];
[clonedSet addObject:clonedRelatedObject];
}
} else {
[cloned setValue:[source valueForKey:relName] forKey:relName];
}
}
return cloned;
}
@end
答案 5 :(得分:6)
我修改了Derrick's answer,这对我来说非常有用,可以支持iOS 5.0和Mac OS X 10.7中提供的ordered relationships:
//
// NSManagedObject+Clone.h
//
#import <CoreData/CoreData.h>
#ifndef CD_CUSTOM_DEBUG_LOG
#define CD_CUSTOM_DEBUG_LOG NSLog
#endif
@interface NSManagedObject (Clone)
- (NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context
excludeEntities:(NSArray *)namesOfEntitiesToExclude;
@end
//
// NSManagedObject+Clone.m
//
#import "NSManagedObject+Clone.h"
@interface NSManagedObject (ClonePrivate)
- (NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context
withCopiedCache:(NSMutableDictionary **)alreadyCopied
excludeEntities:(NSArray *)namesOfEntitiesToExclude;
@end
@implementation NSManagedObject (Clone)
- (NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context
withCopiedCache:(NSMutableDictionary **)alreadyCopied
excludeEntities:(NSArray *)namesOfEntitiesToExclude {
if (!context) {
CD_CUSTOM_DEBUG_LOG(@"%@:%@ Try to clone NSManagedObject in the 'nil' context.",
THIS_CLASS,
THIS_METHOD);
return nil;
}
NSString *entityName = [[self entity] name];
if ([namesOfEntitiesToExclude containsObject:entityName]) {
return nil;
}
NSManagedObject *cloned = nil;
if (alreadyCopied != NULL) {
cloned = [*alreadyCopied objectForKey:[self objectID]];
if (cloned) {
return cloned;
}
// Create new object in data store
cloned = [NSEntityDescription insertNewObjectForEntityForName:entityName
inManagedObjectContext:context];
[*alreadyCopied setObject:cloned forKey:[self objectID]];
} else {
CD_CUSTOM_DEBUG_LOG(@"%@:%@ NULL pointer was passed in 'alreadyCopied' argument.",
THIS_CLASS,
THIS_METHOD);
}
// Loop through all attributes and assign then to the clone
NSDictionary *attributes = [[NSEntityDescription entityForName:entityName
inManagedObjectContext:context] attributesByName];
for (NSString *attr in attributes) {
[cloned setValue:[self valueForKey:attr] forKey:attr];
}
// Loop through all relationships, and clone them.
NSDictionary *relationships = [[NSEntityDescription entityForName:entityName
inManagedObjectContext:context] relationshipsByName];
NSArray *relationshipKeys = [relationships allKeys];
for (NSString *relName in relationshipKeys) {
NSRelationshipDescription *rel = [relationships objectForKey:relName];
NSString *keyName = [rel name];
if ([rel isToMany]) {
if ([rel isOrdered]) {
// Get a set of all objects in the relationship
NSMutableOrderedSet *sourceSet = [self mutableOrderedSetValueForKey:keyName];
NSMutableOrderedSet *clonedSet = [cloned mutableOrderedSetValueForKey:keyName];
for (id relatedObject in sourceSet) {
//Clone it, and add clone to set
NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context
withCopiedCache:alreadyCopied
excludeEntities:namesOfEntitiesToExclude];
if (clonedRelatedObject) {
[clonedSet addObject:clonedRelatedObject];
}
}
} else {
// Get a set of all objects in the relationship
NSMutableSet *sourceSet = [self mutableSetValueForKey:keyName];
NSMutableSet *clonedSet = [cloned mutableSetValueForKey:keyName];
for (id relatedObject in sourceSet) {
//Clone it, and add clone to set
NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context
withCopiedCache:alreadyCopied
excludeEntities:namesOfEntitiesToExclude];
if (clonedRelatedObject) {
[clonedSet addObject:clonedRelatedObject];
}
}
}
} else {
NSManagedObject *relatedObject = [self valueForKey:keyName];
if (relatedObject) {
NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context
withCopiedCache:alreadyCopied
excludeEntities:namesOfEntitiesToExclude];
[cloned setValue:clonedRelatedObject forKey:keyName];
}
}
}
return cloned;
}
- (NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context
excludeEntities:(NSArray *)namesOfEntitiesToExclude {
NSMutableDictionary* mutableDictionary = [NSMutableDictionary dictionary];
return [self cloneInContext:context
withCopiedCache:&mutableDictionary
excludeEntities:namesOfEntitiesToExclude];
}
@end
答案 6 :(得分:5)
我真的需要解决@derrick在其原始答案中承认的大规模复制问题。我修改了MasonK的版本。这在以前的版本中没有很多优雅;但它似乎解决了我的应用程序中的一个关键问题(类似实体的意外重复)。
//
// NSManagedObject+Clone.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface NSManagedObject (Clone) {
}
-(NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context withCopiedCache:(NSMutableDictionary *)alreadyCopied exludeEntities:(NSMutableArray *)namesOfEntitiesToExclude isFirstPass:(BOOL)firstPass;
-(NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context exludeEntities:(NSMutableArray *)namesOfEntitiesToExclude;
-(NSManagedObject *) clone;
@end
//
// NSManagedObject+Clone.m
//
#import "NSManagedObject+Clone.h"
@implementation NSManagedObject (Clone)
-(NSManagedObject *) clone {
NSMutableArray *emptyArray = [NSMutableArray arrayWithCapacity:1];
return [self cloneInContext:[self managedObjectContext] exludeEntities:emptyArray];
}
- (NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context withCopiedCache:(NSMutableDictionary *)alreadyCopied exludeEntities:(NSMutableArray *)namesOfEntitiesToExclude
isFirstPass:(BOOL)firstPass
{
NSString *entityName = [[self entity] name];
if ([namesOfEntitiesToExclude containsObject:entityName]) {
return nil;
}
NSManagedObject *cloned = [alreadyCopied objectForKey:[self objectID]];
if (cloned != nil) {
return cloned;
}
//create new object in data store
cloned = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
[alreadyCopied setObject:cloned forKey:[self objectID]];
//loop through all attributes and assign then to the clone
NSDictionary *attributes = [[NSEntityDescription entityForName:entityName inManagedObjectContext:context] attributesByName];
for (NSString *attr in attributes) {
[cloned setValue:[self valueForKey:attr] forKey:attr];
}
//Inverse relationships can cause all of the entities under one area to get duplicated
//This is the reason for "isFirstPass" and "excludeEntities"
if (firstPass == TRUE) {
[namesOfEntitiesToExclude addObject:entityName];
firstPass=FALSE;
}
//Loop through all relationships, and clone them.
NSDictionary *relationships = [[NSEntityDescription entityForName:entityName inManagedObjectContext:context] relationshipsByName];
for (NSString *relName in [relationships allKeys]){
NSRelationshipDescription *rel = [relationships objectForKey:relName];
NSString *keyName = rel.name;
if ([rel isToMany]) {
if ([rel isOrdered]) {
NSMutableOrderedSet *sourceSet = [self mutableOrderedSetValueForKey:keyName];
NSMutableOrderedSet *clonedSet = [cloned mutableOrderedSetValueForKey:keyName];
NSEnumerator *e = [sourceSet objectEnumerator];
NSManagedObject *relatedObject;
while ( relatedObject = [e nextObject]){
//Clone it, and add clone to set
NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context withCopiedCache:alreadyCopied exludeEntities:namesOfEntitiesToExclude
isFirstPass:firstPass];
if (clonedRelatedObject != nil) {
[clonedSet addObject:clonedRelatedObject];
[clonedSet addObject:clonedRelatedObject];
}
}
}
else {
NSMutableSet *sourceSet = [self mutableSetValueForKey:keyName];
NSMutableSet *clonedSet = [cloned mutableSetValueForKey:keyName];
NSEnumerator *e = [sourceSet objectEnumerator];
NSManagedObject *relatedObject;
while ( relatedObject = [e nextObject]){
//Clone it, and add clone to set
NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context withCopiedCache:alreadyCopied exludeEntities:namesOfEntitiesToExclude
isFirstPass:firstPass];
if (clonedRelatedObject != nil) {
[clonedSet addObject:clonedRelatedObject];
}
}
}
}
else {
NSManagedObject *relatedObject = [self valueForKey:keyName];
if (relatedObject != nil) {
NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context withCopiedCache:alreadyCopied exludeEntities:namesOfEntitiesToExclude
isFirstPass:firstPass];
if (clonedRelatedObject != nil) {
[cloned setValue:clonedRelatedObject forKey:keyName];
}
}
}
}
return cloned;
}
-(NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context exludeEntities:(NSMutableArray *)namesOfEntitiesToExclude {
return [self cloneInContext:context withCopiedCache:[NSMutableDictionary dictionary] exludeEntities:namesOfEntitiesToExclude isFirstPass:TRUE];
}
@end
答案 7 :(得分:4)
这样的东西? (未经测试)这将是您提到的“手动方式”,但它会自动与模型更改同步,因此您不必手动输入所有属性名称。
斯威夫特3:
extension NSManagedObject {
func shallowCopy() -> NSManagedObject? {
guard let context = managedObjectContext, let entityName = entity.name else { return nil }
let copy = NSEntityDescription.insertNewObject(forEntityName: entityName, into: context)
let attributes = entity.attributesByName
for (attrKey, _) in attributes {
copy.setValue(value(forKey: attrKey), forKey: attrKey)
}
return copy
}
}
<强>目标-C:强>
@interface MyObject (Clone)
- (MyObject *)clone;
@end
@implementation MyObject (Clone)
- (MyObject *)clone{
MyObject *cloned = [NSEntityDescription
insertNewObjectForEntityForName:@"MyObject"
inManagedObjectContext:moc];
NSDictionary *attributes = [[NSEntityDescription
entityForName:@"MyObject"
inManagedObjectContext:moc] attributesByName];
for (NSString *attr in attributes) {
[cloned setValue:[self valueForKey:attr] forKey:attr];
}
return cloned;
}
@end
这将返回一个包含所有属性且没有复制关系的克隆。
答案 8 :(得分:3)
您要求的内容称为“深层复制”。因为它可能非常昂贵(如在无限制的内存使用中)并且很难正确(考虑对象图中的循环),因此Core Data不会为您提供此功能。
然而,通常有一种架构可以避免这种需求。您可以创建一个新实体,而不是复制整个对象图,也可以创建一个新实体,用于封装您复制对象图然后仅引用原始图时所具有的差异(或未来差异)。换句话说,实例化一个新的“定制器”实体,而不是复制整个对象图。例如,考虑一组排屋。每个都有相同的框架和设备,但业主可以定制油漆和家具。不是为每个所有者深度复制整个房屋图,而是为每个所有者提供一个“绘画和家具”实体 - 引用所有者和房屋模型。
答案 9 :(得分:3)
这里有 swift 3 方法:
func shallowCopy(copyRelations: Bool) -> NSManagedObject? {
guard let context = managedObjectContext, let entityName = entity.name else { return nil }
let copy = NSEntityDescription.insertNewObject(forEntityName: entityName, into: context)
let attributes = entity.attributesByName
for (attrKey, _) in attributes {
copy.setValue(value(forKey: attrKey), forKey: attrKey)
}
if copyRelations {
let relations = entity.relationshipsByName
for (relKey, relValue) in relations {
if relValue.isToMany {
let sourceSet = mutableSetValue(forKey: relKey)
let clonedSet = copy.mutableSetValue(forKey: relKey)
let enumerator = sourceSet.objectEnumerator()
while let relatedObject = enumerator.nextObject() {
let clonedRelatedObject = (relatedObject as! NSManagedObject).shallowCopy(copyRelations: false)
clonedSet.add(clonedRelatedObject!)
}
} else {
copy.setValue(value(forKey: relKey), forKey: relKey)
}
}
}
return copy
}
答案 10 :(得分:2)
这称为“深层复制”。因为它可能非常昂贵,许多语言/库不支持它开箱即用,并要求你自己动手。可可不幸的是其中之一。
答案 11 :(得分:0)
此外:
[clone setValuesForKeysWithDictionary:[item dictionaryWithValuesForKeys:[properties allKeys]]];
[clone setValuesForKeysWithDictionary:[item dictionaryWithValuesForKeys:[attributes allKeys]]];
答案 12 :(得分:0)
如果您只想在关系Hierarchie中关联实体,您只需将以下代码添加到Dmitry的解决方案
在此之间
NSString *entityName = [[self entity] name];
下面 if([namesOfEntitiesToExclude containsObject:entityName]){
NSMutableArray *arrayToOnlyRelate = [NSMutableArray arrayWithObjects:@"ENTITY 1",@"ENTITY 2",@"ENTITY 3", nil];
if ([arrayToOnlyRelate containsObject:entityName]) {
return self;
}
答案 13 :(得分:0)
我对此的看法是https://gist.github.com/jpmhouston/7958fceae9216f69178d4719a3492577
将rel.inverseRelationship.name
传递给递归方法,以省略访问反向关系而不是维护一组alreadyCopied
个对象
浅或深拷贝
接受与不克隆关系的密钥路径,但要么省略,要么只是复制,如果逆是多对多关系
有序的,多对多关系的解决方法以向后的顺序结束 - 简单地遍历源实体向后 :)我不确定这是一个好主意还是它甚至一直有效
反馈&amp;评论欢迎,特别是如果有人可以详细说明Benjohn关于错误排序的评论above“解决这个问题的方法是构建完整的有序集,然后使用原始KVO变体进行分配。“并且可以改进我订购的,多对应的解决方法。
另外,我正在使用MagicalRecord,因此我的代码假定,包括提供使用其默认上下文的简单方法。
答案 14 :(得分:0)
Swift 4.0版本
import UIKit
import CoreData
class ManagedObjectCloner: NSObject {
static func cloneObject(source :NSManagedObject, context :NSManagedObjectContext) -> NSManagedObject{
let entityName = source.entity.name
let cloned = NSEntityDescription.insertNewObject(forEntityName: entityName!, into: context)
let attributes = NSEntityDescription.entity(forEntityName: entityName!, in: context)?.attributesByName
for (key,_) in attributes! {
cloned.setValue(source.value(forKey: key), forKey: key)
}
let relationships = NSEntityDescription.entity(forEntityName: entityName!, in: context)?.relationshipsByName
for (key,_) in relationships! {
let sourceSet = source.mutableSetValue(forKey: key)
let clonedSet = cloned.mutableSetValue(forKey: key)
let e = sourceSet.objectEnumerator()
var relatedObj = e.nextObject() as? NSManagedObject
while ((relatedObj) != nil) {
let clonedRelatedObject = ManagedObjectCloner.cloneObject(source: relatedObj!, context: context)
clonedSet.add(clonedRelatedObject)
relatedObj = e.nextObject() as? NSManagedObject
}
}
return cloned
}
}
答案 15 :(得分:0)
快速5
这是基于@Derrick和@Dmitry Makarenko和@masonk的贡献,将所有内容整合在一起,对其进行了改进,并将其变成了适合2020年的解决方案。
。
import CoreData
extension NSManagedObject {
func copyEntireObjectGraph(context: NSManagedObjectContext) -> NSManagedObject {
var cache = Dictionary<NSManagedObjectID, NSManagedObject>()
return cloneObject(context: context, cache: &cache)
}
func cloneObject(context: NSManagedObjectContext, cache alreadyCopied: inout Dictionary<NSManagedObjectID, NSManagedObject>) -> NSManagedObject {
guard let entityName = self.entity.name else {
fatalError("source.entity.name == nil")
}
if let storedCopy = alreadyCopied[self.objectID] {
return storedCopy
}
let cloned = NSEntityDescription.insertNewObject(forEntityName: entityName, into: context)
alreadyCopied[self.objectID] = cloned
if let attributes = NSEntityDescription.entity(forEntityName: entityName, in: context)?.attributesByName {
for key in attributes.keys {
cloned.setValue(self.value(forKey: key), forKey: key)
}
}
if let relationships = NSEntityDescription.entity(forEntityName: entityName, in: context)?.relationshipsByName {
for (key, value) in relationships {
if value.isToMany {
if let sourceSet = self.value(forKey: key) as? NSMutableOrderedSet {
guard let clonedSet = cloned.value(forKey: key) as? NSMutableOrderedSet else {
fatalError("Could not cast relationship \(key) to an NSMutableOrderedSet")
}
let enumerator = sourceSet.objectEnumerator()
var nextObject = enumerator.nextObject() as? NSManagedObject
while let relatedObject = nextObject {
let clonedRelatedObject = relatedObject.cloneObject(context: context, cache: &alreadyCopied)
clonedSet.add(clonedRelatedObject)
nextObject = enumerator.nextObject() as? NSManagedObject
}
} else if let sourceSet = self.value(forKey: key) as? NSMutableSet {
guard let clonedSet = cloned.value(forKey: key) as? NSMutableSet else {
fatalError("Could not cast relationship \(key) to an NSMutableSet")
}
let enumerator = sourceSet.objectEnumerator()
var nextObject = enumerator.nextObject() as? NSManagedObject
while let relatedObject = nextObject {
let clonedRelatedObject = relatedObject.cloneObject(context: context, cache: &alreadyCopied)
clonedSet.add(clonedRelatedObject)
nextObject = enumerator.nextObject() as? NSManagedObject
}
}
} else {
if let relatedObject = self.value(forKey: key) as? NSManagedObject {
let clonedRelatedObject = relatedObject.cloneObject(context: context, cache: &alreadyCopied)
cloned.setValue(clonedRelatedObject, forKey: key)
}
}
}
}
return cloned
}
}
用法:
let myManagedObjectCopy = myManagedObject.copyEntireObjectGraph(context: myContext)
答案 16 :(得分:0)
这是对@Geoff H、@Derrick、@Dmitry Makarenko 和@masonk 的贡献的轻微改进。代码使用了更函数式的风格并抛出了友好的错误?。
NSManagedObject
图(使用 alreadyCopied
缓存)NSManagedObject
的扩展实施DeepCopyError
而不是崩溃 fatalError()
NSManagedObjectContext
本身检索 NSManagedObject
警告:此代码仅经过部分测试!
import CoreData
extension NSManagedObject {
enum DeepCopyError: Error {
case missingContext
case missingEntityName(NSManagedObject)
case unmanagedObject(Any)
}
func deepcopy(context: NSManagedObjectContext? = nil) throws -> NSManagedObject {
if let context = context ?? managedObjectContext {
var cache = Dictionary<NSManagedObjectID, NSManagedObject>()
return try deepcopy(context: context, cache: &cache)
} else {
throw DeepCopyError.missingContext
}
}
private func deepcopy(context: NSManagedObjectContext, cache alreadyCopied: inout Dictionary<NSManagedObjectID, NSManagedObject>) throws -> NSManagedObject {
guard let entityName = self.entity.name else {
throw DeepCopyError.missingEntityName(self)
}
if let storedCopy = alreadyCopied[self.objectID] {
return storedCopy
}
let cloned = NSEntityDescription.insertNewObject(forEntityName: entityName, into: context)
alreadyCopied[self.objectID] = cloned
// Loop through all attributes and assign then to the clone
NSEntityDescription
.entity(forEntityName: entityName, in: context)?
.attributesByName
.forEach { attribute in
cloned.setValue(value(forKey: attribute.key), forKey: attribute.key)
}
// Loop through all relationships, and clone them.
try NSEntityDescription
.entity(forEntityName: entityName, in: context)?
.relationshipsByName
.forEach { relation in
if relation.value.isToMany {
if relation.value.isOrdered {
// Get a set of all objects in the relationship
let sourceSet = mutableOrderedSetValue(forKey: relation.key)
let clonedSet = cloned.mutableOrderedSetValue(forKey: relation.key)
for object in sourceSet.objectEnumerator() {
if let relatedObject = object as? NSManagedObject {
// Clone it, and add clone to the set
let clonedRelatedObject = try relatedObject.deepcopy(context: context, cache: &alreadyCopied)
clonedSet.add(clonedRelatedObject as Any)
} else {
throw DeepCopyError.unmanagedObject(object)
}
}
} else {
// Get a set of all objects in the relationship
let sourceSet = mutableSetValue(forKey: relation.key)
let clonedSet = cloned.mutableSetValue(forKey: relation.key)
for object in sourceSet.objectEnumerator() {
if let relatedObject = object as? NSManagedObject {
// Clone it, and add clone to the set
let clonedRelatedObject = try relatedObject.deepcopy(context: context, cache: &alreadyCopied)
clonedSet.add(clonedRelatedObject as Any)
} else {
throw DeepCopyError.unmanagedObject(object)
}
}
}
} else if let relatedObject = self.value(forKey: relation.key) as? NSManagedObject {
// Clone it, and assign then to the clone
let clonedRelatedObject = try relatedObject.deepcopy(context: context, cache: &alreadyCopied)
cloned.setValue(clonedRelatedObject, forKey: relation.key)
}
}
return cloned
}
}