ios - Core Data: Multiple conditions inside relational aggregate operations -
i have sqlite table used core data following elements:
name: john
lastname: foobar
age: 23
name: bob
lastname: baz
age: 37
name: peter
lastname: fooqux
age: 32
name: john
lastname: bar
age: 29
those in to-many relationship object "company". need query database , retrieve company objects employees called "john" last name not contain "foo".
i did go far make following predicate:
[nspredicate predicatewithformat:@"any employee.name = 'john'"];
how filter companies johns don't have "foo" in last names?
assuming named to-many relationship 'employees', here sample fetch companies fall under requested criteria.
appdelegate *appdelegate = [[uiapplication sharedapplication] delegate]; nsmanagedobjectcontext *context = [appdelegate managedobjectcontext]; //fetch companies nsentitydescription *entitydesc = [nsentitydescription entityforname:@“company” inmanagedobjectcontext:context]; nsfetchrequest *request = [[nsfetchrequest alloc] init]; [request setentity:entitydesc]; nspredicate *predicate = [nspredicate predicatewithformat:@"(subquery(employees, $x, $x.name = ‘john’ , not $x.lastname = ‘foo’).@count > 0)"]; //set predicate [request setpredicate:predicate]; nserror *error; nsarray *filteredcompanies = [context executefetchrequest:request error:&error];
Comments
Post a Comment