views:

57

answers:

1

alt text

Hi,

Although I have a lot of experience in database development, I'm having a hard time conceptualizing linking relationships in Core Data. As I understand it, the many relationship is an NSSet attached to the one file. After reading the documentation, I've understood part of it and gotten it to work in the first import in my code below.

I have a data model into which I perform two separate imports using the XMLParser. The first import loads Events and Categories from the same XML file within the same import like so:

if (thisTagIsForOneTable) {
            // Insert object for the one-entity (Events)
    self.eventsObject = [NSEntityDescription insertNewObjectForEntityForName:@"Events" inManagedObjectContext:xmlManagedObjectContext];
    return;
}   

if (thisTagIsForManyTable) {
            // Insert object for many-entity (EventCategories)
    self.categoriesObject = [NSEntityDescription insertNewObjectForEntityForName:@"EventCategories" inManagedObjectContext:xmlManagedObjectContext];
    return;
}

    ......
    // Set attribute values depending upon whether the tag is for the one-entity or the many-entity.
[self.xxxObject setValue:trimmedString forKey:attributeName];
    ......

    // Set the relationship. This works great!
if (thisTagIsForManyTable) {
    NSMutableSet *manyRecordSet = [self.eventsObject  mutableSetValueForKey:@"categories"]; // My relationship name.
    [manyRecordSet addObject:self.categoriesObject];

    }

    // Save the context. Voila.

The above works fine. The second import loads EventLocations separately in a different part of the application so I need to set it's to-one relationship to Events. This is where I'm not too sure. Should the steps be?

// Step A) Create (insert) new EventLocations object.
    self.eventLocationsObject = [NSEntityDescription insertNewObjectForEntityForName:@"EventLocations" inManagedObjectContext:xmlManagedObjectContext];



    // Step B) Locate and get a reference to the the related one-entity's object (Events) by ID?  I have a custom class for Events. 
   // This seems to work but I'm taking a performance hit and getting a compiler warning below. The method returnObjectForEntity does a query and returns an NSManagedObject. Is this correct?
    Events *event = (Events *)[self returnObjectForEntity:@"Events" withID:[oneRecordObject valueForKey:@"infIDCode"]];


    // Step C) Set the relationship to the Events entity. 
if (event) {

    [event addLocationsObject:self.eventLocationsObject];

        // Compiler warning: incompatible Objective-C types 'struct NSManagedObject *', expected 'struct EventLocations *' when passing argument 1 of 'addLocationsObject:' from distinct Objective-C type


}
    // Save the context.

I'm not too sure about steps B and C. Any help would be appreciated. Thanks.

+1  A: 

Step B: Please tell us the compiler warning. To Speed things up you could create a cache (NSDictionary) with all Events and their @"infIDCode" as Keys before starting the import. This would speed things up as long as you can make sure no Events are added/deleted/changed within the import phase.

Step C: self.eventLocationsObject should probably be declared as EventLocations*.

In general your import should work this way

Martin Brugger
The compiler warning is in Step C:
b.dot
OK. Just to understand this better: What I'm doing is inserting an new object in the entity (Step A) and then adding the object into the set in the one entity's relationship (Step C)?
b.dot
my comment on step C should fix the compiler warning. yes you add an new object to the context and afterwards add it to a relationship (set) as needed
Martin Brugger
Thanks a lot Martin.
b.dot