0

I have 3 NSManagedObjets; Person, Stuff, and Collection.

enter image description here

I want to use a NSPredicate to get a list of all Collections that ThePerson has.

Example: Scott has objectA and objectB which are in collection Letters and object1 which is in collection Numbers.

I want to be able to do a fetch request and get back collection Letters and Numbers.

I tried:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY stuffs.persons == %@", person];

And:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(stuffs, $s, ANY $s.persons == %@)", scott];

Any suggestions?

Padin215
  • 7,444
  • 13
  • 65
  • 103

2 Answers2

1

Your SUBQUERY syntax is wrong (for a full explanation, see this answer or this answer). It should be something like:

SUBQUERY(stuffs, $s, ANY $s.persons == %@).@count > 0
Community
  • 1
  • 1
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
1

Since it seems that you already have a reference to a ThePerson object, you don't need to do a fetch or use a predicate. You can traverse the relationships you've declared to get the collections. You can get all of the Collections that ThePerson has by using:

NSSet *collections = [person valueForKeyPath:@"stuffs.collections"];
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • There is another condition that I forgot to mention: Scott's Letter `Collection` may contain objectA and objectB, while Jen's Letter Collection may only contain objectA. With that requirement, I don't believe this solution would work? Or would I be wrong? I'm sorry I didn't state this in the question originally. – Padin215 Aug 29 '13 at 13:31