1
votes

I have an entity matches which has a related to-many entity sets. I want to get a count of how many sets have the attribute 'set_finished' set to YES for a particular match. I'm trying to do this with:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY set_finished == YES"]; NSUInteger numberOfFinishedSets = [[[match valueForKeyPath:@"sets"] filteredArrayUsingPredicate:predicate ] count];

The second line crashes with this error, which I don't understand. Can anyone shed some light on this for me? Thanks.

2010-12-20 13:17:13.814 DartScorer[2154:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSFaultingMutableSet filteredArrayUsingPredicate:]: unrecognized selector sent to instance 0x617fb20'

4

4 Answers

0
votes

You should use filteredSetUsingPredicate: instead of filteredArrayUsingPredicate since the object is a set, not an array.

0
votes

_NSFaultingMutableSet is kind of an empty set (if you will print the set you will see that xcode prints the entity name but not its content. it does that because :

Faulting reduces the amount of memory your application consumes. A fault is a placeholder object that represents a managed object that has not yet been fully realized, or a collection object that represents a relationship:

A managed object fault is an instance of the appropriate class, but its persistent variables are not yet initialized. A relationship fault is a subclass of the collection class that represents the relationship. Faulting allows Core Data to put boundaries on the object graph. Because a fault is not realized, a managed object fault consumes less memory, and managed objects related to a fault are not required to be represented in memory at all.

i think you should create a Set to hold the objects you want to filter, and then filter the array tou have created.

hope it helps shani

0
votes
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY set_finished == YES).@count"];  
NSUInteger numberOfFinishedSets = [[match valueForKeyPath:@"sets"] filteredSetUsingPredicate:predicate];

This will just return the count of how many objects there are instead of actually retrieving the objects from disk and counting them.

-1
votes

Try wrapping the string YES in single quotes "... 'YES' "

(assuming the field contains the strings 'YES' and 'NO' and not 1 | 0)