How are to-many attributes specified in a fetched

2019-07-21 10:05发布

问题:

I have two entities, Parent and Child. The Parent entity has a to-many relationship to Child named "children." Child has a String attribute named "childName."

I want to make a fetched property on Parent, let's call it "specialChild" that returns a Child with a particular name, let's say "Special". The following predicates return an empty set when I access the fetched property:

  • children.childName == "Special"
  • SUBQUERY(children, $eachChild, $eachChild.childName = "Special").@count > 0
  • SUBQUERY(children, $eachChild, ANY $eachChild.childName = "Special").@count > 0

I believe I'm messing up the predicate somehow, because I'm still pretty inexperienced with them. (and I can find zero documentation from Apple on "SUBQUERY") How am I supposed to specify "the child whose childName is Special" in the Parent's fetched property predicate?

Yes, I am calling -refreshObject:mergeChanges: but I still receive an empty result. Yes, the destination entity is Child.

回答1:

What you want is parent==$FETCH_SOURCE AND childName=="Special". This gets any Child whose childName is "Special" and whose parent is the object looking up its special children.

Attributes in a fetched property predicate must exist on the destination entity. Here the destination is Child, so you can't use children since that only exists on Parent.

The $FETCH_SOURCE part corresponds to where you'd use self if you wrote the predicate in code. Without that you get every special child, not just the ones attached to the originating Parent. It says, the parent attribute of the child must be the specific instance looking up the fetched property value.