核心数据多对多关系 NSPredicate [英] Core Data Many-to-Many Relationship NSPredicate

查看:31
本文介绍了核心数据多对多关系 NSPredicate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多对多关系的数据模型,例如 EntityA <-->>实体B<<-->实体C.我曾经使用不同的搜索条件查询 EntityA,并使用 NSCompoundPredicateNSPredicate 数组.在我想使用 EntityC 查询 EntityA 的谓词之一.我尝试使用以下 SUBQUERY 但它没有用.

searchPredicate=[NSPredicate predicateWithFormat:@"(0 != SUBQUERY(EntityB, $B, (0 != SUBQUERY($B.EntityC, $EntityC, $EntityC.name like %@).@count)).@count)", 姓名]

我得到了以下异常,

<前>由于未捕获的异常NSInternalInconsistencyException"而终止应用程序,原因:无法使用非集合对象执行集合评估."

有什么我遗漏的吗?我将不胜感激.

莎拉

解决方案

(我在弄清你的谓词时遇到了很多麻烦,所以请持保留态度.)

我认为你把这弄得太复杂了.您应该使用键路径进行简化.每个 EntityB 与 EntityC 都具有一对一的关系,因此要搜索 EntityB 对象,您只需检查 EntityB.entityCRelationshipName.entityCAttribute.所以就像:

ALL B.EntityC.name like %@

无论如何,(如果我正确阅读谓词):

SUBQUERY($B.EntityC, $EntityC, $EntityC.name like %@)

只能返回单个对象而不是集合.因此,谓词将无法对其执行计数.这就是您的错误消息的含义.

我建议在存储谓词之前使用数据模型编辑器中的谓词编辑器对谓词进行散列.

I have a data model with a many-to-many relationship like EntityA <-->> EntityB <<--> EntityC. I used to query EntityA with different search criteria and I use NSCompoundPredicate with an array of NSPredicates. On one of the predicate I wanted to query EntityA using EntityC. I tried to use the following SUBQUERY but it did not work.

searchPredicate=[NSPredicate predicateWithFormat:@"(0 != SUBQUERY(EntityB, $B, (0 != SUBQUERY($B.EntityC, $EntityC, $EntityC.name like %@).@count)).@count)", name]

And I got the following exception,

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'Can't perform collection evaluate with non-collection object.'

Is there anything I'm missing. I would appreciate any help.

Sarah

解决方案

(I had a lot of trouble puzzling out your predicate so take this with a grain of salt.)

I think your making this overly complicated. You should simplify with keypaths. Each EntityB has a to-one relationship to a EntityC so to search on EntityB objects all you need to do is check EntityB.entityCRelationshipName.entityCAttribute. So something like:

ALL B.EntityC.name like %@

In any case, (if I'm reading the predicate correctly) this:

SUBQUERY($B.EntityC, $EntityC, $EntityC.name like %@)

can only return a single object instead of a collection. Therefore, the predicate will not be able to perform a count on it. That is what your error message means.

I suggest using the predicate editor in the Data Model editor to hash out the predicates before you store them.

这篇关于核心数据多对多关系 NSPredicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆