I’ve managed to tie myself in knots with a Core Data predicate – probably because I’m trying to convert from SQL to Core Data concepts in my head…
I’ve got two NSManagedObject classes which are joined with a one-to-many relationship:
Customer <---->> Agreements
Customer has-many Agreements
Agreement has-one Customer
The Agreement class has a boolean property called paymentRequired.
What I’m trying to do is to retrieve all Customers that have an Agreement with the paymentRequired property set to true.
In SQL terms, I think this could be done with a subquery:
select CUSTOMER.NAME from CUSTOMER where CUSTOMER.ID in (select distinct AGREEMENT.CUSTOMER from AGREEMENT where AGREEMENT.PAYMENTREQUIRED = 1)
I’ve got no problems retrieving the Agreements with the flag set from Core Data, but I’m struggling to then get the distinct Customers.
Can anyone tell me where I’m going wrong?
You will find the documentation in the short Predicate Programming Guide. On the very first page (Introduction) there is a solution to your problem. I recommend reading it in order to finally manage to mentally detach yourself from the SQL idiom.