Core Data and NSPredicate for a to-many relationship
23 Jul 2009, 07:53
Massimo Cafaro (4 posts)
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.
In order to fetch the 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
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.
While this should be fine, at least reading the documentation for Core Data and NSPredicate, I get the following error message:
Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Unsupported predicate
If I use the equivalent predicate
no exception is thrown, however the code does not work as expected: the occurrence for today, which should not be excluded, is instead excluded.
I am not sure also how to test for the case excludedOccurrences == nil: the following predicate
causes at runtime the exception
to-many key not allowed here
However, since the excludedOccurrences relationship is optional, I also need to test if it is nil.
How do I deal with this? Thank you in advance.
27 Jul 2009, 05:46
Marcus S. Zarra (253 posts)
A date always has a time component even if you do not add one. To get the results you are expecting you need to do an inclusion time query such as
include two date objects, one for 12:00am today and one for 11:59pm today.
If you are familiar with working with databases, consider a NSDate to be the same thing as a Timestamp in database lingo.
|You must be logged in to comment|