是不是可以复制核心数据NSManagedObject?

时间:2021-09-18 20:12:09

I am trying to copy a NSManagedObject that holds a core data instance, but I am being some very weird behaviours when doing so, its like the objects are somehow linked to each other after the copy and changes to one also affects the other.

我正在尝试复制一个包含核心数据实例的NSManagedObject,但是当我这样做时,我就是一些非常奇怪的行为,它就像对象在复制之后以某种方式相互链接,并且对一个对象的更改也会影响另一个。

NSManagedObject *originalTransactionRow = [self.transactionRowsRows objectAtIndex:indexPath.row];
NSManagedObject *originalTransactionHeader = [self.transactionRowsHeader objectAtIndex:0];
NSString *originalOrderNumberLocalStr = [originalTransactionRow valueForKey:@"orderNumber"];

NSString *returnOrderNumber = [NWTillHelper getNewOrderNumber];
NSManagedObject *returnOrderHeader = nil;
NSManagedObject *returnOrderRow = nil;

returnOrderHeader = [NSEntityDescription insertNewObjectForEntityForName:@"OrderHead" inManagedObjectContext:context];
returnOrderRow = [NSEntityDescription insertNewObjectForEntityForName:@"OrderRow" inManagedObjectContext:context];

returnOrderHeader = originalTransactionHeader;
returnOrderRow = originalTransactionRow;

NSError *error = nil;
if(![context save:&error]) {
   if([NWTillHelper isDebug] == 1) {
   NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
   return;
   }
}

1 个解决方案

#1


1  

If you're referring to these lines:

如果您指的是这些行:

returnOrderHeader = originalTransactionHeader;
returnOrderRow = originalTransactionRow;

Those don't make copies. But it's not about Core Data or managed objects-- that's not how Objective-C works when you're detailing with object references. When you have a declaration like NSManagedObject *returnOrderHeader, the * means that you're saving a pointer to the object. Copying the pointer value doesn't copy the object data, it copies the pointer value. You end up with two variables with the same pointer, which means they're really the same object.

那些不复制。但它不是关于核心数据或托管对象 - 当您使用对象引用进行详细设计时,这不是Objective-C的工作方式。当你有一个类似NSManagedObject * returnOrderHeader的声明时,*表示你正在保存一个指向该对象的指针。复制指针值不会复制对象数据,它会复制指针值。最终得到两个具有相同指针的变量,这意味着它们实际上是同一个对象。

If you want to make a copy of an object, you need to take extra steps. If your object's class conforms to the NSCopying protocol then you can just call copy on the object. But managed objects don't do that, so you need to create a new instance and then write your own code to copy property values from the old instance to the new one.

如果要复制对象,则需要执行额外的步骤。如果您的对象的类符合NSCopying协议,那么您只需在对象上调用copy即可。但是托管对象不这样做,因此您需要创建一个新实例,然后编写自己的代码以将属性值从旧实例复制到新实例。

#1


1  

If you're referring to these lines:

如果您指的是这些行:

returnOrderHeader = originalTransactionHeader;
returnOrderRow = originalTransactionRow;

Those don't make copies. But it's not about Core Data or managed objects-- that's not how Objective-C works when you're detailing with object references. When you have a declaration like NSManagedObject *returnOrderHeader, the * means that you're saving a pointer to the object. Copying the pointer value doesn't copy the object data, it copies the pointer value. You end up with two variables with the same pointer, which means they're really the same object.

那些不复制。但它不是关于核心数据或托管对象 - 当您使用对象引用进行详细设计时,这不是Objective-C的工作方式。当你有一个类似NSManagedObject * returnOrderHeader的声明时,*表示你正在保存一个指向该对象的指针。复制指针值不会复制对象数据,它会复制指针值。最终得到两个具有相同指针的变量,这意味着它们实际上是同一个对象。

If you want to make a copy of an object, you need to take extra steps. If your object's class conforms to the NSCopying protocol then you can just call copy on the object. But managed objects don't do that, so you need to create a new instance and then write your own code to copy property values from the old instance to the new one.

如果要复制对象,则需要执行额外的步骤。如果您的对象的类符合NSCopying协议,那么您只需在对象上调用copy即可。但是托管对象不这样做,因此您需要创建一个新实例,然后编写自己的代码以将属性值从旧实例复制到新实例。