Kevin
Kevin

Reputation: 17586

Core Data Relationship sort by child's properties

I have two Core Data entities, say a Manager and an Employee. Each Manager has two and only employees. The Employees have a hour integer of when they clock in and when they clock out. How can I create a Predicate that checks if the managers have employes who clock in before a certain time and clock out after a certain time?

If the relationship was expressed as an array instead of set I could do this:

[NSPredicate predicateWithFormat:@"SELF.employees[0] > %i && SELF.employees[1] < %i", clockIn, clockOut];

I also could separate the check in time and check out time as two separate entities with the same properties, but this seems to go against the whole concept of OOP.

[NSPredicate predicateWithFormat:@"SELF.employees.in.hour > %i && SELF.employees.out.hour < %i", clockIn, clockOut];

I also tried adding a category to Manager that returns the "in", but the I get a Keypath not found in entity... error.

Upvotes: 0

Views: 94

Answers (1)

Wain
Wain

Reputation: 119041

You can use the aggregate operators ALL, ANY, SOME and NONE:

[NSPredicate predicateWithFormat:@"ALL employees.inTime > %i && ALL employees.outTime < %i", clockIn, clockOut];

You haven't said what the inTime and outTime attributes are named so replace as appropriate.

Upvotes: 1

Related Questions