title: How to correctly setup a NSPredicate for a to-many relationship when using Core Data?
body: <p>I have a Core Data model in which a Task entity includes an optional to-many relationship ExcludedDays to the ExcludedDay entity. One of the properties of ExcludedDay is day, which is an NSDate object. The ExcludedDay entity has an inverse mandatory to-one relationship to the Task entity.</p>
  
  <p>In order to fetch the tasks for a specified day, I need to make sure that the specified day does not appear as the day property of any ExludedDay entity.</p>
  
  <p>I started trying</p>
  
  <pre><code>NSPredicate *dayIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"ALL excludedDays.day != %@", today];
  </code></pre>
  
  <p>However, despite what the documentation says, ALL does not work and the application throws an exception: Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Unsupported predicate.</p>
  
  <p>After posting the same question in this forum, I was able to devise the following predicate with the help of various people:</p>
  
  <pre><code>NSPredicate * dayIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"excludedDays.@count == 0 || (excludedDays.@count &gt; 0 &amp;&amp; NONE excludedDays.day == %@))", today];
  </code></pre>
  
  <p>While this worked at first, I have just discovered that this only works when the ExcludedDay entity contains ONLY one day. As soon as the ExcludedDay entity contains more than one day for the same task, this predicate stops working. As a result, a task is selected for a day even though the day appears as a day in the ExcludedDay entity, which is of course wrong. The problem is not tied to the property day being a NSDate object: replacing day with the corresponding NSString or equivalently with an integer, I still face the same issue and incorrect behaviour.</p>
  
  <p>What is the correct way to implement the predicate in this case? May this be a bug related to the ANY aggregate operator when using core data?
  Thank you in advance, this is now driving me crazy.</p>