ios - Core Data filter to many ordered relationship with predicate -
i have core data model task , list, list has to-many ordered relationship task, , task has inverse to-many relationship list. need retrieve tasks given list in correct order, filtered match predicate. example, if priority
field on task,
nspredicate* predicate = [nspredicate predicatewithstring:@"priority == high"] list* list; // fetched
i can accomplish
nsarray* alltasks = list.tasks; nsarray* sometasks = [alltasks filteredarrayusingpredicate:predicate];
but requires pulling tasks memory. fetch tasks directly
nsfetchrequest* req = [[nsfetchrequest alloc] initwithentityname:@"task"]; [req setpredicate:[nspredicate predicatewithformat:@"list == %@ , priority == high", list]]; nsarray* sometasks = [context executefetchrequest:req error:&error];
but task order not preserved.
is there way filter relationship fault predicate?
i think following method work, though inefficient:
- fetch required
tasks
using desired predicate, ensuring fetch request'sreturnsobjectsasfaults
property set true. - create new predicate format "self in %@", passing array of tasks (from step 1) argument.
- filter ordered set (
list.tasks
) using new predicate.
that way predicate evaluated in store, without faulting in tasks, while order still determined ordered relationship.
Comments
Post a Comment