Reputation: 4695
I'm a bit confused on how to correctly make a copy of a Core Data object (just attributes, not relationships). Say that I have my object A, with an NSNumber x, and a NSString s. Is this correct way to copy this:
MyObject* B = (MyObject*)[NSEntityDescription insertNewObjectForEntityForName:@"MyObject" inManagedObjectContext:moc];
B.x = A.x;
B.s = A.s;
Or this:
MyObject* B = (MyObject*)[NSEntityDescription insertNewObjectForEntityForName:@"MyObject" inManagedObjectContext:moc];
B.x = [A.x copy];
B.s = [A.s copy];
If I update the attributes of A in the future, I don't want the attributes of B to change.
Upvotes: 1
Views: 359
Reputation: 36497
Both NSNumber
and NSString
are immutable; if A's attributes changed in the future, it wouldn't change the existing NSNumber
and NSString
objects, it would replace them. From that point of view, your first example is perfectly sufficient.
In fact, your second example is likely to leak memory if you aren't running under garbage collection, since you're never releasing your copied versions of A's attributes.
Upvotes: 1