5
votes

I have been looking for this but I found nothing and I think it's unsupported by Core Data. Anyway, let's see it:

I have 3 entities: Ticket, TicketLine and CheffLine.

A Ticket contains multiple TicketLines (Ticket.lines). **CheffLine** inherits from TicketLine.

Now I want to fetch the tickets (by means of a NSFetchedResultsController), but filtering the tickets that have at least one CheffLine with a cheffStatus greater than 0. The problem is that cheffStatus is an attribute of the CheffLine entity, so I make this predicate:

[NSPredicate predicateWithFormat:@"0 != SUBQUERY(lines, $x, $x.cheffStatus > 0).@count)"];

But when executed, I get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to generate SQL for predicate (0 != SUBQUERY(lines, $x, $x.cheffStatus > 0).@count) (problem on RHS)'

I noticed that if the query is done using $x.someVariableInTheTicketLineEntity instead of $x.cheffStatus, it works. I also tried to use CAST, but obtained the same result

[NSPredicate predicateWithFormat:@"0 != SUBQUERY(lines, $x, CAST($x, 'CheffLine').cheffStatus > 0).@count)"];

Is there any way of doing this query? Remember that it must work inside a NSFetchedResultsController.

1
I didn't see any solution, so I'm going to workaround on this by changing my fetch request to fetch directly CheffLine objects, and then do all the hard work in the NSFetchedResultsController delegate functions - quarac
It worked for this case, but now I have another issue of the same kind with other sub entity of CheffLine. I think the way to go is not using child entities in Core Data, something impossible for me at the moment. - quarac

1 Answers

4
votes

Ok, I found that there's no solution. Look at this:

How can you reference child entity name in a predicate for a fetch request of the parent entity?

The way to go is to fetch the results in 2 separate requests and then merge manually, so forget about the NSFetchedResultsController.