I’m trying to retrieve data from an Entity in Core Data where I know the value of a related Entity.
IE:
Entity1
-attrib1.1
-attrib1.2
-relationship1
Entity2
-attrib2.1
-relationship1
Entity1 has a to-many relationship to Entity2 on relationship1.
I’m trying to get the value of Entity2 where Entity1.attrib1.1 = XXX.
I tried using NSPredicate, but I’m not sure how, if possible, to write the syntax in the predicateWithFormat method.
If this doesn’t make sense, sorry. I’ll try to clear up if needed.
I have searched google and here, but haven’t found anything. Maybe my eyes are giving out? 😉
I think you’re trying find instances of
Entity2whereEntity1.attrib1.1==XXXandEntity1.relationship1contains a reference to theEntity2instance. From your description, it’s not clear ifEntity2.relationship1is the inverse ofEntity1.relationship1. If not, you really should create the inverse relationship and set it as such in the data modeler. Unless you really know what you’re doing and are sure you do not need the inverse relationship, Core Data will not work as you expect unless the inverse relationship exists.Once you have the inverse relationship from
Entity2toEntity1(let’s call itinverseRelationshipfor sake of example), you can perform a fetch request onEntity2using anNSPredicateinstance with the format stringif the inverse is a to-one relationship or
if the inverse is also a to-many relationship.