2

I have a Core Data object that has an NSOrderedSet of Core Data objects in it called hierarchyItems. I want to create a fetch request that will check to see if the hierarchyItems set contains an object that is also in another NSMutableSet of Core Data objects.I would like to create a NSPredicate or NSExpression that has the same functionality as

[obj.hierarchyItems intersectsSet: setOfItems];
user3200440
  • 23
  • 1
  • 4
  • It does not look like a duplicate to me. This question is about set intersection, which is actually easier to test for than set equality. – Martin R Jan 16 '14 at 06:15
  • @MartinR: set equality testing is quite trivially derived from set intersection – njzk2 Feb 28 '14 at 17:01
  • @njzk2: I do not quite understand what you mean. The question was about Core Data predicates (which have only a limited set of available operators). As you can see from the answer to the "possible duplicate", finding all objects whose relationship is *equal* to a given set is more complicated than the solution to this question. – Martin R Feb 28 '14 at 18:07

1 Answers1

4

To fetch the object for which hierarchyItems has a non-empty intersection with setOfItems, use a fetch request with the predicate

[NSPredicate predicateWithFormat:@"ANY hierarchyItems IN %@", setOfItems]
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Sadly no this does not work. I have tried this to no avail. – user3200440 Jan 16 '14 at 21:06
  • @user3200440: Well, I have tested it and it worked for me. - Can you provide more information? How are the entities defined, relationships etc. Do you get an error message, no results, wrong results ... ? – Martin R Jan 16 '14 at 21:18
  • I agree that should work perfectly and I don't know why it doesn't. There are no error messages, I just get an empty array back. Before you ask yes there are overlapping objects between the two sets. – user3200440 Jan 17 '14 at 16:37
  • @user3200440: Just to locate the problem: Does it work if you fetch all objects (without any predicate) and then filter the fetched array using the above predicate? – Martin R Jan 17 '14 at 20:56