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

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

问题描述

我有一个具有多对多关系的数据模型,例如 EntityA< - >> EntityB< - > EntityC 。我用来查询 EntityA 不同的搜索条件,我使用 NSCompoundPredicate 与一个数组 NSPredicate s。在其中一个谓词上,我想使用 EntityC 查询 EntityA 。我试图使用以下 SUBQUERY ,但它没有工作。

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

我有以下异常

 
由于未捕获异常而终止应用程序'NSInternalInconsistencyException',原因:
'无法使用非集合对象执行集合评估。'

有什么我缺少。

解决方案

(我曾经很多麻烦使你的谓词困惑,所以把这与一粒盐)。



我认为你的这个过于复杂。您应该使用关键路径简化。每个EntityB与EntityC有一对一的关系,所以你需要做的是检查 EntityB.entityCRelationshipName.entityCAttribute 。例如:

 所有B.EntityC.name如%@ 
pre>

在任何情况下,(如果我正确读取谓词):

  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天全站免登陆