I have the following data model:
Group(name, color, activities*) // one group may have many activities
Activity(startTime, endTime, location, Group*) // startTime and endTime are NSDate, group is an inverse relationship
so if I want to get all Groups who have at least one activity which on on a specified NSDate, say theTime, what predicate should I construct?
My thoughts:
- first get all activities
- then put their groups into a Set in order to remove duplicate Groups
- convert the Set to an array
so:
[NSPredicate predicateWithFormat:@"%@ >= startTime AND %@ <= endTime", theTime]
fetch an array of Activities then:
for (Activity *activity in activities)
{
[theSet add:activity.group];
}
so I got an array of unique Groups that have at least one activity on theTime?
Is there any other way to just do a fetch without post-processing?
Thanks!
EDIT:
Can I do:
[NSPredicate predicateWithFormat:@"%@ >= activities.startTime AND %@ <= activities.endTime", theTime]
???
If I understood well your question, yes.
You can do it fecthing against
Groupinstead ofActivityentity. In this manner you can avoid to fetch firstActivityies enitity and then apply a filter. Sinceactivitiesis not a scalar value, you need a predicate modifier (ANY,ALL, etc.) or a SUBQUERY.About your question I would do the following:
About the predicate, I suppose yuo want to find
Groups that containActivityies included in a specificified range.Try and let me know. Hope that helps.