开发者

I'm trying to get a count of records from a related entity that match a criteria

开发者 https://www.devze.com 2023-01-31 23:17 出处:网络
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 w

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'


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


_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


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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号