我有两种类型的实体:Subjects 和Correspondents。它们都通过一对多关系相互关联。我想要一个fetchRequest可以传递给它的单曲,NSFetchedResultsController它会返回:
subject具有多个 的所有scorrespondent。- 所有
correspondent具有subjects 的 s 仅属于它们。
在尝试了各种各样的事情之后,我认为不可能进行一次同时返回Subjects 和Correspondents 的 fetch,所以我转向 StackOverflow 并发现其他人建议您有一个实体,它除了与您要返回的两个实体。
所以我创建了第三种类型的实体,我称之为s,每个实体都与 a和 aFolder具有可选的一对一关系。它还有两个属性和,它们是跟踪是否设置的布尔值。SubjectCorrespondenthasCorrespondenthasSubjectSubjectCorrespondent
所以我写了这个返回Folder实体的谓词:
(hasCorrespondent == 1 AND ANY correspondent.subjects.correspondents.@count == 1)
OR
(hasSubject == 1 AND subject.correspondents.@count >= 1)
这个问题是我收到一个错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Unsupported function expression count:(correspondent.subjects.correspondents)
那么,关于我做错了什么有什么建议吗?我怎样才能完成我想要的?我应该分享任何其他细节吗?
更新
根据 Martin 的建议,我将有问题的部分更改为:
SUBQUERY(correspondent.subjects, $s, $s.correspondents.@count == 1).@count > 0
但这产生了一个新错误:
Keypath containing KVC aggregate where there shouldn't be one;
failed to handle $s.correspondents.@count
在谷歌搜索之后,我发现建议添加一个检查被枚举的集合是否至少有一个对象,但是将有问题的行修改为此并没有改变我的错误消息(据我所知,它什么也没做):
correspondent.subjects.@count > 0 AND
SUBQUERY(correspondent.subjects, $s, $s.correspondents.@count == 1).@count > 0