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 Entity2
where Entity1.attrib1.1==XXX
and Entity1.relationship1
contains a reference to the Entity2
instance. From your description, it's not clear if Entity2.relationship1
is the inverse of Entity1.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 Entity2
to Entity1
(let's call it inverseRelationship
for sake of example), you can perform a fetch request on Entity2
using an NSPredicate
instance with the format string
inverseRelationship.attrib1.1 == XXX
if the inverse is a to-one relationship or
ANY inverseRelationship.attrib1.1 == XXX
if the inverse is also a to-many relationship.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With