0

I have the following data model:

Person -(OneToMany)-> Task -(OptionalOneToOne)-> Completion

and I need a predicate to return me the persons without incomplete tasks and another for the opposite. A task is completed if there's a completion entity associated and not completed if that reference is nil. I was tempted to use ALL task.completion = nil and NONE task.completion = nil but that's a mistake since the core data framework wraps everything in arrays or sets and they cannot contain a list of nils, so the only way to really solve that is using SUBQUERYs and that's way outside my comfort zone. Plus, I can't find expressive examples of SUBQUERY inside NSPredicate but I know the answer lies there, if this is trivial for anyone please help me break this deadlock.

Thanks in advance

Pedro Borges
  • 1,568
  • 1
  • 14
  • 25
  • `task.completion.count == 0`, maybe? Or, `task.completion[SIZE] == 0`. – duci9y Oct 05 '14 at 12:47
  • completion is a OneToOne relation it's an object not a collection. – Pedro Borges Oct 05 '14 at 12:56
  • One to One, you say? `task.completion == nil` would do. Core Data only wraps One to Many or Many to Many relationships with sets. And not arrays. – duci9y Oct 05 '14 at 12:57
  • Please read the post again. I don't need the list of tasks that are completed I need the list of PERSONS with NO tasks NOT completed or in better english the list of persons with no incomplete tasks. – Pedro Borges Oct 05 '14 at 12:59
  • The predicates you were tempted to use would work, try them. – duci9y Oct 05 '14 at 13:02
  • Is this the same problem as in your previous question http://stackoverflow.com/questions/26188309/nspredicate-does-not-work-as-expected-for-all-relationship-innerrelationship ? – Martin R Oct 05 '14 at 13:04
  • I tried them and they didn't work, I explain why in the post, you can read about it in more detail here: http://stackoverflow.com/questions/13193872/core-data-nspredicate-any-key-path-nil Check the answer by Dave DeLong – Pedro Borges Oct 05 '14 at 13:06
  • @MartinR it kind of is an update to it yes, the link above answered my previous question and it evolved into this. Thanks for reminding just deleted it. – Pedro Borges Oct 05 '14 at 13:13

1 Answers1

1

A predicate with a subquery like this should work

SUBQUERY(tasks, $t, $t.completed == nil).@count == 0

to find all people without incomplete tasks.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • And that translates to SUBQUERY(tasks, $t, $t.completed == nil) > 0 for all people WITH incomplete tasks. Perfect, that was precisely what I was looking for, thank you my friend. – Pedro Borges Oct 05 '14 at 19:58