Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
Many to many relationship with the use of a lookup entity
I'm hoping you can help me sort out an issue I'm experiencing with retrieving data from a entity via a relationship. I'll start off by explaining the relationships between the entities I'm currently working with:
I'm working with 4 Entities, named the following: Sections, Routes, SectionRoutes, and Grades.
The relationships between these entities are as follows:
Sections has a one-to-many relationship with SectionRoutes. Routes has a one-to-many relationship with SectionRoutes. Grades has a one-to-one relationship with Routes.
So the end goal which has resulted in the above design is as follows: One section can have many routes, and one route can be in many sections. So what I've done is I've created a lookup table that stores a section and a route to relate the two (namely SectionRoutes). So in my app I'm retrieving a list of routes from a web service that I'm then inserting into my model that will be associated to a section - this is all done on a separate thread in it's own NSManagedObjectContext so as to not block the main thread. So when inserting a new route, I already know which section it should belong to so I simply populate the Route properties as necessary, I then insert a new record into the SectionRoutes entity and add my new route and section NSManagedObjects. In the same method I also need to relate a Grade to the route I'm inserting. The web service returns a Grade ID which I use in an NSPredicate to retrieve the correct Grade from my local model (the Grades entity at this point is already populated). I then assign the Grade I've retrieved to my grade relationship for the new route. Once this is all complete, I then run a query to retrieve all the SectionRoute objects into a NSFetchedResultsController so as to populate a tableView to display all these routes.
Here is the code which creates a new route as described above:
// this is where I grab the Grade NSManagedObject for the given grade ID that I get from the web service Grades *grade = [Grades getGradeForGradeServerID:gradeID moc:moc];
// this outputs the correct value NSLog(@\"colour == %@\", grade.colour);
// this is where I associate the returned grade to the new route I'm creating (one to one relationship) newRoute.grade = grade;
// I then retrieve the section this route belongs to using the NSManagedObject ObjectID passed to this worker thread Sections *parentSection = (Sections*)[moc objectWithID:sectionObjectID];
// insert this new route with it's parent section into the lookup table SectionRoutes *sectionRouteObj = [SectionRoutes insertSectionWithRoute:parentSectionObj route:newRoute moc:moc];
So now onto my problem, I'm retrieving all the SectionRoutes and populating a tableView with routes for a given section. In my cellForRowAtIndexPath: I retrieve the SectionRoute for the given indexPath - I then extract the route which is related to my Routes entity to populate labels etc, I then also try extract the Route grade by doing the following: sectionRoute.route.grade. For some reason this is returning (null). I'm certain that when I associate a grade to a route (in my code above) that the grade itself is returned correctly from the local model, but for some reason when I try access it via the Route object as described it returns a null.
This is what the code in my cellForRowAtIndexPath: method that I'm using to retrieve the data I need looks like: