title: Core Data, NSPredicate and to-many key
body: <p>I have a Core Data model in which a Task entity includes an optional to-many relationship excludedOccurrences. One of the properties of excludedOccurrences is start, which is an NSDate object. The ExcludedOccurrence entity has an inverse mandatory to-one relationship to the Task entity.</p>
  
  <p>In order to fetch tasks for a specified day, I need to make sure that the specified day does not appear as the start property of any ExcludedOccurrence entity. One of the sub-predicates I  am trying to use is therefore</p>
  
  <pre><code>NSPredicate *occurrenceIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"(ALL excludedOccurrences.start != %@))", today];
  </code></pre>
  
  <p>where today is a NSDate object for today including only the day, month and year components. All of the excluded occurrences start properties also include just the day, month and year components. </p>
  
  <p>While this should fine, at least reading the documentation for Core Data and NSPredicate, I get the following error message:</p>
  
  <p>Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate</p>
  
  <p>If I use the equivalent predicate</p>
  
  <pre><code>NSPredicate *occurrenceIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"!(ANY excludedOccurrences.start == %@))", today];
  </code></pre>
  
  <p>no exception is thrown, however the code does not work as expected: the occurrence for today, which should not be excluded, is instead excluded.</p>
  
  <p>I am not sure also how to test for the case excludedOccurrences == nil: the following predicate</p>
  
  <pre><code>NSPredicate *nilPredicate = [NSPredicate predicateWithFormat: @"(excludedOccurrences == nil)"];
  </code></pre>
  
  <p>causes at runtime the exception</p>
  
  <p>to-many key not allowed here</p>
  
  <p>However, since the excludedOccurrences relationship is optional, I also need to test if it is nil.</p>
  
  <p>How do I deal with this? Thank you in advance.</p>