本文介绍了Core Data SUBQUERY和NSFetchedResultsController的Keypath错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,如果这是重复,20分钟的搜索没有产生这种确切的情况或解决方案。

Apologies if this is a duplicate, 20 minutes of searching didn't yield this exact situation or a solution.

我有一个Core数据堆栈, code> XClass , YClass ZClass XClass YClass 具有一对多的关系。 YClass ZClass 有一对多的关系。

I have a Core Data stack with three classes XClass, YClass, and ZClass. XClass has a one-to-many relationship with YClass. YClass has a one-to-many relationship with ZClass.

使用 NSFetchedResultsController 的实例,我试图获取所有 XClass 的实例,其中至少有1个 YClass 至少有一个 ZClass

Using an instance of NSFetchedResultsController, I'm trying to fetch all instances of XClass for which at least 1 YClass has at least 1 ZClass.

定义如下:

// ...stuff
NSFetchRequest * fetchRequest = [NSFetchRequest new];

NSEntityDescription * entity = [NSEntityDescription entityForName:NSStringFromClass([XClass class])
                                           inManagedObjectContext:managedObjectContext];
fetchRequest.entity = entity;

fetchRequest.predicate = 
[NSPredicate predicateWithFormat:@"0 < SUBQUERY(yObjects, $y, $y.zObjects.@count > 0).@count"];

// ..instantiate NSFetchedResultsController and perform fetch

异常与消息:终止应用程序由于未捕获异常'NSInvalidArgumentException',原因:包含KVC的键集合聚合其中不应该有一个;未能处理$ y.zObjects。@ count'

在其他地方,我已成功获取 YObject 实例与谓词 [NSPredicate predicateWithFormat:@zObjects。@ count> 0];

Elsewhere, I've successfully fetched YObject instances with the predicate [NSPredicate predicateWithFormat:@"zObjects.@count > 0"];

有人可以指点我的错误吗?非常感谢。

Can someone point me to what I'm doing wrong? Thank you much.

推荐答案

在SUBQUERY的谓词字符串中使用集合运算符时,NSPredicate对象此外,您似乎无法对子查询谓词中的集合进行操作。这是除非变量表示谓词中的集合。

It seems that NSPredicate objects violently when you use collection operators in the predicate string for the SUBQUERY. It also appears that you cannot operate on a collection in the subquery predicate. That is unless the variable represents a collection in the predicate.

BAD: SUBQUERY(yObjects,$ y,$ y.zObjects == NIL)> 0

OK: SUBQUERY(yObjects,$ y,SUBQUERY($ y.zObjects $ z,$ z = = NIL)> 0) 0

以下表达式将所有非nil对象添加到SUBQUERY返回的过滤集合。换句话说,返回的集合将包含具有ZClass实例化的XClass的所有实例。

The following expression will add all non-nil objects to the filtered collection returned by the SUBQUERY. In other words, the returned collection will contain all instances of XClass with instantiations of ZClass.

[NSPredicate predicateWithFormat:@"yObjects.@count > 0 AND (SUBQUERY(yObjects, $y, (SUBQUERY($y.zObjects, $z, $z != NIL).@count > 0)).count > 0)"];

这篇关于Core Data SUBQUERY和NSFetchedResultsController的Keypath错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 18:46