views:

281

answers:

1

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? ;)

+2  A: 

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.

Barry Wark
You are the MAN! Yes I do have inverse relationships set up. I was actually just trying out exactly what you wrote with a twist.I was trying:Entity.reverseRelationship.attrib1.1 == XXXThen I tried it without Entity and it worked! I then came here and saw your post! Thanks again!!
RoLYroLLs
Sorry, I'm a newuser so i can't upvote you yet. Only got 11 rep. Thanks again!
RoLYroLLs
I'm glad you figured it out on your own too. Good luck!
Barry Wark
Thanks. Actually my first post on any iPhone programming. I've been able to research and find a solution to all my issues for the past year. =) Imagine my frustration when I couldn't find the answer for this question. I was probably not searching the right keywords.Cheers!
RoLYroLLs