Вопрос

I have three separate entities, (r are relationships):

1) CURRENCY

name

_

countries (r 1 to many)

2) COUNTRY

name

_

currency (r)

exchanges (r 1 to many)

3) EXCHANGE

name

_

countries (r)

The relationship from CURRENCY to COUNTRY is ONE TO MANY.

The relationship from COUNTRY to EXCHANGE is ONE TO MANY.

I need to load my NSFetchedResultsController with all the CURRENCY (without duplicates) where the COUNTRY has at least one EXCHANGE.

Example:

Currencies: Euro, United States Dollar, British Pound, Tiberium (doesn't exist!)

Countries: France, Germany, Italy, United States, United Kingdom

Exchanges: Euronext (France, Euro), Xetra (Germany, Euro), Milan Stock Exchange (Italy, Euro), London Stock Exchange (United Kingdom, British Pound)

The expected results is: British Pound, Euro, United States Dollar

I tried something like this but it didn't work:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Currency"];
request.predicate = [NSPredicate predicateWithFormat: @"countries.exchanges.@count > 0"];

The code crashes due to uncaught exception: 'NSInvalidArgumentException', reason: 'Unsupported function expression count: (countries.exchanges)'

What is the right NSPredicate I should use?

Это было полезно?

Решение

The problem is the nested to-many relationship, which requires the use of a SUBQUERY:

[NSPredicate predicateWithFormat:@"SUBQUERY(countries, $c, ANY $c.exchanges != NULL).@count > 0"]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top