0

My second post here. I'm still very new to core data for IOS. I've read through the apple developer page, but I still don't understand exactly how to implement the code.

I have 2 entities with relationship: Student <----->> StudentInfo

Student properties (strings): firstname, lastname, studentid

StudentInfo properties (strings): itemA, itemB, itemC

Since each Student has many StudentInfos, how would I go about accomplishing the task of finding only the entities with (itemA == "abc") and displaying the related Student's studentid along with itemC?

edit

NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:entitydesc];

NSPredicate *predi = [NSPredicate predicateWithFormat:@"studentid like %@", self.idTextField.text];
[req setPredicate:predi];

NSError *error;
NSArray *matchingData = [self.managedObjectContext executeFetchRequest:req error:&error];

if (matchingData.count <= 0) {
    self.testLabel.text = @"nada found";
} else {
    NSString *firstName;
    NSString *lastName;

    for (NSManagedObject *obj in matchingData) {
        firstName = [obj valueForKey:@"firstname"];
        lastName = [obj valueForKey:@"lastname"];
    }
    self.testLabel.text = [NSString stringWithFormat:@"%@%@", firstName, lastName];

}

I got this much so far and this works great for accessing data from the main Student entity, but how would I go about accessing attributes of a specific case of entity StudentInfo?

... Sorry for any improper formatting, and thank you in advance for the help!

user2951
  • 67
  • 1
  • 6
  • 1
    I don't have any code setup but you need to attach a `NSPredicate` on the `fetchRequest` for the `entity`. Check this SO question for examples. http://stackoverflow.com/questions/1347776/how-to-correctly-setup-a-nspredicate-for-a-to-many-relationship-when-using-core – LyricalPanda Jun 03 '14 at 20:08
  • @LyricalPanda I read up on the link, is there any sample code you can suggest, or any more guidance? I'm fooling around with it now to see if I can figure some more of it out. – user2951 Jun 04 '14 at 05:47

1 Answers1

0

First of all, you must model the relationship between your entities on the model designer. Choose Student, click on the '+' sign on the relationships section, and make it point to Student Info. After creating it, open its properties on the right side pane and change its type to "To many".

When you generate the classes for your model (Editor -> Create NSManagedObject subclass) Xcode will automatically generate a property of type NSSet that contains all objects that belong to the relationship, and a few more methods do make it easy to add or remove objects from it.

So, if you generate classes as Student, StudentInfo, and has a relationship from Student to (many) StudentInfo, named studentInfo, you'll have something similar as (pay attention that names may vary, check the generated header file):

Student* yourStudent = ....;
StudentInfo* info1 = .....;
StudentInfo* info2 = ....;

[yourStudent addStudentInfoObject: info1];
[yourStudent addStudentInfoObject: info2];
// and so on

Now, you can iterate over the NSSet property that represents your relationship in order to find your objects:

for(StudentInfo* info in yourStudent.studentInfo) {
    //do whatever you want with info, like comparing its name   
}

Thanks

  • Perhaps my post wasn't clear. I already have the two entities created with the NSManagedObject subclasses. What I was asking was how to search through all of the (Student)s' and all of their relationships, such that once a match of (itemA == "abc") is found, I can display the studentid and say, an itemC. – user2951 Jun 03 '14 at 22:15