Given a core data entity setup like this
- Entity A
- aB -> B (many to 1)
- aC -> C (many to 1)
- children -> Child (many to many)
- Entity B
- children -> Child (many to many)
- Entity C
- children -> Child (many to many)
- Entity Child
I'm trying to do a query to find all Entity A's such that a set of Child entities TestChildren is found across all the reachable Children. Something like this example for a matching A entity, A1
TestChildren Set
- Child1
- Child2
- Child3
- Child4
A1 Entity
- aB -> B1
- aC -> C1
- children
- -> child1
- -> child3
- B1 Entity
- children
- -> child2
- children
- C1 Entity
- children
- -> child4
- children
So all 4 children have been found across the various children relationships in each class.
It seems that to test for these sets I have to perform subqueries over each Children relationship, but I don't think I can express what I want with the NSPredicate format.. perhaps its just not doable at all with NSPredicates?
An idea I had was to do something like this
predicateString += "((SUBQUERY(aB.children, $child, $child in %@).@count + SUBQUERY(aC.children, $child, $child in %@).@count + SUBQUERY(children, $child, $child in $@).@count) == $@)"
predicateVars.append(contentsOf: [testChildren, testChildren, testChildren, testChildren.count])
Where the total of matching counts across all the children relationships have to match.. unfortunately if the same child could be present in more than one A,B, or C then this wouldn't be correct as the same child matched in two different classes would allow the total to incorrectly match.
Regardless it seems that adding up counts is not something that is even possible in NSPredicates?
The only way to really do this properly is to be able to generate a total set of all children across the 3 classes, but again there doesn't seem to be a way to express this in an NSPredicate?
Worse case I can do a simpler query and perform more logic on the returned results, but ideally I'd try to do as much heavy lifting as possible in the query.
Thanks in advance