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 their 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:
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!
Open the Data Model inspector (choose View > Inspectors > Show Data Model Inspector). Select the source entity from the Entities list, then select the new relationship in the Relationships list.
Most apps need just a single managed object context. The default configuration in most Core Data apps is a single managed object context associated with the main queue. Multiple managed object contexts make your apps harder to debug; it's not something you'd use in every app, in every situation.
NSPersistentContainer simplifies the creation and management of the Core Data stack by handling the creation of the managed object model ( NSManagedObjectModel ), persistent store coordinator ( NSPersistentStoreCoordinator ), and the managed object context ( NSManagedObjectContext ).
A description of search criteria used to retrieve data from a persistent store.
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 :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With