Ali
Ali

Reputation: 645

NSPredicate count on a relationship attribute

I have actually a core data entity called Show and for every show I have a relation ship called Schedule that contains an attribute called schedules which is an array.

What I want to do is to create a Predicate that checks for a given show's schedule if it has more than 1 item.

To do That I tried Two solutions :

  1. NSPredicate * predicate = [NSPredicate predicateWithFormat:@"schedule.schedules > 1];

  2. NSPredicate * predicate = [NSPredicate predicateWithFormat:@"schedule.schedules.@count > 1];

But None of them would work.

NB : schedules attribute is an stored in my Core Data as Transformable and declared as an id variable.

Upvotes: 2

Views: 590

Answers (2)

Pablo A.
Pablo A.

Reputation: 2052

Did you try this?

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"@count.schedule.schedules > 1];

Upvotes: 0

Wain
Wain

Reputation: 119031

A transformable attribute is stored as binary data, and it isn't unpacked until the container entity is loaded into memory. So, you can't use any kind of predicate based on a transformable attribute in a fetch. It will only work to filter the results of the fetch.

Cheap solution is to store a simple integer attribute with the count and use that.

Better solution is likely to add / change your entities so you use a relationship instead of a transformable.

Upvotes: 2

Related Questions