我試圖複製一個NSManagedObject,它包含一個核心數據實例,但是我這樣做時會出現一些非常奇怪的行爲,就像這些對象在複製之後以某種方式鏈接到對方,並且更改爲另一個也會影響另一個。不可能複製核心數據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;
}
}
關於這個問題的很多實驗可以在這裏找到:https://stackoverflow.com/questions/2730832/how-can-i-duplicate-or-copy-a-core-data-managed-object – danh