I have an in memory managed object context called importMoc that I use to import records (e.g. employees). I have parsed a file and set up the employee objects in importMoc with one very important exception. The user confirms they want to process %d employees but I can't quite figure out how or when to set the "parent" relationship of the employees (e.g. setting th开发者_运维百科eir department).
For my purposes they will all be imported into the same department (which the user has already implicitly selected).
Obviously I can't set up the relationships across the two contexts so do I:
- Create a department in importMoc and then when I merge changes merge the "import" department with the "real" department?
- 2) Merge the employees and then fetch all the freshly imported employees (somehow!!!) and set their department then?
- 3) Some other solution that I have overlooked?
It seems like a simple problem but for some reason (laziness? tiredness? stupidity?) I can't figure out how to go about it! Everything I've tried so far seems far too elaborate and complicated!
Thanks in advance!
If the Department
objects have already been saved to a persistent store, then you can bring them into another managed object context. Since your objects will all have to live in the same persistent store anyway (as cross-store relationships are not allowed), you should be able to simply fetch the ones you need into importMoc
.
For example:
foreach (NSDictionary *record in employeeRecords) {
NSManagedObject *employee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:importMoc];
// Configure employee however you do that
NSString *managerID = [record objectForKey:@"someKeyThatUniquelyIdentifiesTheManager"];
NSFetchRequest *managerFetchRequest = [[NSFetchRequest alloc] init];
[managerFetchRequest setEntity:[NSEntity entityForName:@"Manager" inManagedObjectContext:importMoc]];
[managerFetchRequest setPredicate:[NSPredicate predicateWithFormat:@"managerProperty == %@", managerID]];
NSArray *managers = [importMoc executeFetchRequest:managerFetchRequest error:nil]; // Don't be stupid like me and catch the error ;-)
[managerFetchRequest release];
if ([managers count] != 1) {
// You probably have problems if this happens
}
[employee setValue:[managers objectAtIndex:0] forKey:@"manager"];
}
You could also just do a single fetch request to get all of the managers into importMoc
and then filter that array to locate the right one each time. That would probably be a lot more efficient. In other words, don't do what I just told you to do above :-)
1/ assume employee record X has name Y and department id 15 (i.e. it refers the department with id 15 via a relation)
2/ load department with department id 15 from the managed object context
3/ create an employee that refers the object created at (2)
i.e. (note: sample code only, probably won't compile):
NSEntityDescription * departmentED = [NSEntityDescription entityForName:@"Department" inManagedObjectContext:moc];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(id == %@)", deptId];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:departmentED];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
/* create employee */
NSEntityDescription * employeeED = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:moc];
NSManagedObject * employeeMO = [[[NSManagedObject alloc] initWithEntity:employeeED insertIntoManagedObjectContext:moc] autorelease];
/* set employee department to department instance loaded above */
[receiptObject setValue:[array objectAtIndex:0] forKey:@"department"];
精彩评论