Hi would like to duplicate an object from my core data db. Right now I’m using
movement2 = [NSEntityDescription
insertNewObjectForEntityForName:@"Movement"
inManagedObjectContext:self.managedObjectContext];
movement2.name = movement.name;
movement2.value = movement.value;
movement2.date = movement.date;
...
and it works. but…
Is there any way to copy all the values of movement to movement2, in one line of code?
NSManagedObject, unlike NSObject, provides an API to iterate over its attributes and relationships. Or, rather, it’s entity description does. It isn’t a one-liner, though.
See the documentation for more details.
This will be enough to clone most of the objects. If database structure is correct, then copying relationships this way, their inverse ones will be updated as well. So, if your Movement had a relationship with, say, MovementDirection, and MovementDirection has an inverse 1-to-many relation
parentMovements, thisparentMovementsset will have bothmovementandmovement2inside after you call the code above.