1
votes

I have 4 entities. ArticleGroup, Article, WarehouseStok, Warehouse.

I need to fetch articleGroups which have articles(NSSet) which belong to warehouseStocks(NSSet) which belong to warehouse(single).

    request.predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(articles, $a, SUBQUERY($a.warehouseStocks, $y, $y.warehouse == %@))", warehouse];

I got this error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "SUBQUERY(articles, $a, SUBQUERY($a.warehouseStocks, $y, $y.warehouse == %@))"'

How do I construct such a predicate? Is it possible at all?

1

1 Answers

7
votes

A SUBQUERY returns a set of matching objects. A predicate must returns true or false, for example "SUBQUERY(...).@count > 0".

In your case that would be:

[NSPredicate predicateWithFormat:@"SUBQUERY(articles, $a, SUBQUERY($a.warehouseStocks, $y, $y.warehouse == %@).@count > 0).@count > 0", warehouse];

But I think that you can simplify your predicate to (untested):

[NSPredicate predicateWithFormat:@"SUBQUERY(articles, $a, ANY $a.warehouseStocks.warehouse == %@).@count > 0", warehouse];