I have a model with 2 entities: RealEstate and Container
Containers object are already saved onto the persistent store with the following hierarchy:
Container 1
Container 2
Container 3
Container 4
Container 5
Container 6
Each container has a RealEstate owner (in a given hierarchy the realEstate is always the same) Now I would like create a copy of this hierarchy changing for each containers the owner realEstate. After some tests it seems to me that it's not a trivial problem.
This is a simplified scheme of the model:
RealEstate (entity)
-------------------
name (string attribute)
containers (relation)
Container (entity)
------------------
level (int attribute)
name (string attribute)
parent (self relation to another container)
subcontainers (relation - set of containers)
realEstate (relation)
Basically each containers has a subcontainers relation with self, and a parent relation so for example Container 1 has no parent but subcontainers=[container 2, container 3] etc...
I have 2 questions:
- if I want to copy a single attribute (NSNumber) should I copy the attribute before assign it to the copied container with something like [newContainer setLevel:[[container level] copy]] because NSNumber is actually a pointer, or it's ok to assign [newContainer setLevel:[container level]] ??
- how to copy a relation?? I cannot simply copy the subcontainers with [newContainer setSubcontainers:[container subcontainers]] !!
This is what I'm doing:
- (void)copyContainersFromRealEstate:(RealEstate *)sourceRealEstate toRealEstate:(RealEstate *)destinationRealEstate {
// read all RealEstate Containers to copy
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"realEstate == %@", sourceRealEstate];
[request setPredicate:predicate];
[request setEntity: [ NSEntityDescription entityForName:@"Container"
inManagedObjectContext:destinationRealEstate.managedObjectContext] ];
NSError *error;
NSArray *results = [destinationRealEstate.managedObjectContext executeFetchRequest:request
error:&error];
if (results == nil) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[request release];
// *******************************************************
// copy each caontainer changing the real estate
Container *newContainer;
for (Container *container in results) {
newContainer = (Container *)[NSEntityDescription insertNewObjectForEntityForName:@"Container"
inManagedObjectContext:destinationRealEstate.managedObjectContext];
[newContainer setRealEstate:destinationRealEstate];
[newContainer setLevel:[container level]];
[newContainer setSubcontainers:[container subcontainers]]; // WRONG
[newContainer setName:[container name]];
[newContainer setParent:[container parent]]; // WRONG
}
// *******************************************************
}
Any help will be appreciated. Thanks