我正在尝试从匹配条件的相关实体获取记录计数

发布于 2024-10-08 15:08:58 字数 561 浏览 0 评论 0原文

我有一个实体匹配,它与许多实体集相关。我想计算有多少组将特定匹配的属性“set_finished”设置为“是”。我正在尝试这样做:

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

第二行因此错误而崩溃,我不明白。有人能为我解释一下吗?谢谢。

2010-12-20 13:17:13.814 DartScorer[2154:207] * 终止 应用程序由于未捕获的异常 'NSInvalidArgumentException',原因: '-[_NSFaultingMutableSet FilteredArrayUsingPredicate:]: 无法识别的选择器发送到实例 0x617fb20'

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷你仙 2024-10-15 15:08:59

您应该使用 filteredSetUsingPredicate: 而不是 filteredArrayUsingPredicate,因为该对象是一个集合,而不是一个数组。

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

提笔书几行 2024-10-15 15:08:59

_NSFaultingMutableSet 是一种空集(如果您要打印该集,您将看到 xcode 打印实体名称而不是其内容。它这样做是因为:

故障会减少内存量
您的应用程序消耗。一个错误是
代表一个占位符对象
尚未管理的对象
完全实现,或集合对象
代表一种关系:

托管对象故障是一个实例
适当的类别,但其
持久变量还没有
已初始化。关系错误是
集合类的子类
代表关系。断层
允许核心数据设置边界
对象图。因为故障是
未实现,托管对象错误
消耗更少的内存,并进行管理
与故障相关的对象不是
需要在内存中表示
完全没有。

我认为你应该创建一个 Set 来保存你想要过滤的对象,然后过滤你创建的数组。

希望有帮助
沙尼

_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

月隐月明月朦胧 2024-10-15 15:08:59
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY set_finished == YES).@count"];  
NSUInteger numberOfFinishedSets = [[match valueForKeyPath:@"sets"] filteredSetUsingPredicate:predicate];

这只会返回有多少对象的计数,而不是实际从磁盘检索对象并对其进行计数。

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.

涫野音 2024-10-15 15:08:59

尝试将字符串 YES 用单引号 "... 'YES' " 括起来

(假设该字段包含字符串 'YES' 和 'NO' 而不是 1 | 0)

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文