Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up relationships while importing core data?

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:

  1. Create a department in importMoc and then when I merge changes merge the "import" department with the "real" department?
  2. 2) Merge the employees and then fetch all the freshly imported employees (somehow!!!) and set their department then?
  3. 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!

like image 896
Matthew Avatar asked Oct 20 '09 03:10

Matthew


People also ask

How do you add relationships in Core Data?

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.

Can we have multiple managed object contexts in Core Data?

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.

What is Persistentcontainer in Core Data?

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 ).

What is Nsfetchrequest in Swift?

A description of search criteria used to retrieve data from a persistent store.


1 Answers

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 :-)

like image 149
Alex Avatar answered Oct 27 '22 20:10

Alex