jsrivo
jsrivo

Reputation: 167

In a Core Data entity, how do I use a source entity's attribute in its fetched property's predicate?

I am using the visual editor for creating a Core Data model. I can't figure out how to set a fetched property's predicate so that it uses the source entity's attributes in the predicate.

For example, I have EntityA with an attribute searchId. I also have EntityB with an attribute id. What I want to do is find all instances of EntityB whose id is equal to EntityA's searchId atrribute. When I try to edit the predicate, the drop-down list for the left-hand side and right-hand side only contain the keys for the destination, which in this case is EntityB, and no way to select any key from EntityA.

I know that you can just create the predicate programmatically, but I was wondering if there's a way to do it using the visual editor.

Upvotes: 3

Views: 1887

Answers (1)

TechZen
TechZen

Reputation: 64428

It's a bit of a pain.

First, in the predicate editor control click in the empty space to the right of the predicate's last field and select "variable".Then enter "FETCH_SOURCE.attributeName" in the field.

FETCH_SOURCE (or $FETCH_SOURCE in code) is a special variable that indicates the object originating the fetch for the fetched property. There is also FETCHED_PROPERTY ($FETCHED_PROPERTY) which will return the actual NSFetchedPropertyDescription object.

So, your predicate would in text look like:

id==$FETCH_SOURCE.searchId

Apple is apparently not very proud of this functionality because they have buried any references to it deep in the mythic catacombs of Cupertino. It took me four days to figure it out the first time.

Upvotes: 6

Related Questions