Search code examples
ioscore-dataone-to-manynsfetchrequest

How to fetch records from a class which has parent class reference?


I'm new to CoreData structure, I have two classes one is "Person.h" and another is "Education.h" which has one to many relations ship Person<--->> Education`.

Here's the attributes for each classes,

Person.h

personID (unique) Number
pName String
pAge Number
educations Set

here, p stands for person

Education.h

educationID (unique) Number
eName String
eState Number
eStarted String
eCompleted String
eCenterName String
eBy Person

here, e stands for education

Ok, now I want to fetch (all / some) education details for a Person. I've successfully inserted records in both the classes with proper inputs. How to get this done? Any suggestion? Please consider me to correcting, even if this flow would not clear to you (or its wrong).

Thanks,

Hagile


Solution

  • Normally you'd have a Core Data relationship on Person that points to the Education entity, configured as to-many. Then once you have an instance of Person, you just look up the value of that relationship like you'd look up the value of any property. You get back a collection of zero or more related Education instances, and you don't need to do an additional fetch.

    Your eBy relationship on Education suggests that you're thinking of this as if you were working with SQL. With Core Data it's normal to have a to-many relationship defined on the entity that has the relationship (and indeed, eBy should really have an inverse relationship).