0

I have look around for something similar with no luck so I am going to try and explain my trouble and paste some code. I have an application that uses Core Data and I can save and retrieve data from their respective textFields with the exception of my (to many relationships). I believe these are saved and returned as sets when fetched. I have read up on NSSet and looked at some code but with still do not understand how to code it.

Thanks for any help. Hudson

- (IBAction) findContact
{
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Place" 
                                              inManagedObjectContext:context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
[fetchRequest setEntity:entity];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"placeName = %@", placeName.text];
[fetchRequest setPredicate:pred];
Place *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];
if ([objects count] == 0) {
    status.text = @"No matches";
}else{

    matches = [objects objectAtIndex:0];
    address.text = [matches valueForKey:@"address"];
    city.text = [matches valueForKey:@"city"];
    state.text = [matches valueForKey:@"state"];
    zip.text = [matches valueForKey:@"zip"];
    phone1.text = [matches valueForKey:@"phone1"];
    email.text = [matches valueForKey:@"email"];
    website.text = [matches valueForKey:@"website"];
    phone2.text = [matches valueForKey:@"phone2"];
    about.text = [matches valueForKey:@"about"];
    photoName.text = [[matches photo]valueForKey:@"photoName"];
    status.text = [NSString  stringWithFormat:@"%d matches found", [objects count]];
    NSSet *groupForSections = [groupForSections valueForKey:@"sections"];
    for (Group *group in groupForSections) {
        NSLog(@"group name = %@", [groupForSections valueForKey:@"groupName"]);
        groupName.text = [group valueForKey:@"groupName"];
    NSSet *sectionForPlaces = [sectionForPlaces valueForKey:@"places"];
    for (Section *section in sectionForPlaces) {
        sectionName.text = [section valueForKey:@"sectionName"];
        NSLog(@"section name = %@", [section valueForKey:@"sectionName"]);
        }
    }
    }
    }

![enter image description here][1]

1 Answers1

0

According to your follow-up comments, Place does not actually have a to-many relationship to Section, and Section does not actually have a to-many relationship to Group. Instead, Group has a to-many relationship (sections) with Section and section's inverse to-1 relationship is called group. Also, Section has a to-many relationship (places) with Place and Place's inverse to-1 relationship is called section.

If all those assumptions are correct (and without seeing your model it's hard to know if that is correct), your code should now be something like:

NSManagedObject *sectionForPlace = [matches valueForKey:@"section"]; 
sectionName.text = [sectionForPlace valueForKey:@"sectionName"];

NSManagedObject *groupForSection = [sectionForPlace valueForKey:@"group"];
gromName.text = [groupForSection valueForKey:@"groupName"];

This code doesn't directly access the fields through the NSManagedObject classes you've created, which should also work if you prefer that way.

Tim Dean
  • 8,253
  • 2
  • 32
  • 59
  • I appreciate the help, but I still don't understand how to return the sectionName. Specifically: sectionName.text = [section valueForKey:@"sectionName"]; I see how you can loop thur them using the Fast Enumeration and a NSLog statemant, but I need to retrieve it back into the textField as part of the "match" that gets returned if there is one. – Hudson Reed Nov 17 '11 at 17:09
  • Updated the sample to set sectionName.text rather than just logging it. The thing you need to think of is how you want the text field to be filled if the Place that matched has multiple sections. The updated code above will set your text field to the section name in the set of sections for the place. If you want to do something with the "many" part of the relationship you'll have to be more specific. – Tim Dean Nov 17 '11 at 18:08
  • I appreciate your help, but it still does not return the sectionName or groupName. Nor does it print the NSLog statements to the console. Also, my model at this time does not allow a Place to have more than one Section. FYI, I deleted the SQlite database and re-entered the data just in case. – Hudson Reed Nov 17 '11 at 18:43
  • Your original problem stated that "Place has a to-many relationship to Section/places" but now you're saying that your model does not allow a Place to have more than one Section? Those two statements cannot both be true. Did you initially mean to say that Section has a to-many relationship called "places" with Place? If so you'll need to provide more info on how the inverse of that relationship is defined. I recommend you update your post to more completely and accurately describe your model. – Tim Dean Nov 17 '11 at 19:11
  • This is a graphic of the model. Please disregard other things on this link because they have changed. I am very sorry for the confusion as I tried to upload an image of the model, but was not allowed. http://www.topgradedesigns.com/blog/index.html – Hudson Reed Nov 17 '11 at 19:35
  • If I am reading your data model correctly, I think you have your to-many relationships backwards. The names of your relationships suggest you want each group to have many sections, and each section to have many places. But your model says that each place can have many sections and each section can have many groups. In other words, the "to-many" is modeled on the wrong-side of the relationships. Switch them around and I think you'll have more luck – Tim Dean Nov 17 '11 at 20:04
  • I changed the model as you suggested and deleted the sqlite file. Entered new data and it still did not work. Please look at my code above when you have a chance. Sorry for your trouble. – Hudson Reed Nov 17 '11 at 20:26
  • I have updated my response above to show the code based on a corrected understanding of what's in you're model now. – Tim Dean Nov 18 '11 at 00:10