我有一个解决我的问题的方法,但是我真的想了解为什么我有这个问题以及如何解决它。
我有一个与另一个实体B相关的实体A,并且A的一些行在一个字段中有一个特殊标记。
我想数一下与B相关的A中有多少个有这个特殊标记
一切正常,如果在我创建NSSet之后我计算集合:
NSSet *productsSet = currentList.ingredients;
int countProducts = productsSet.count;
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"self IN %@ AND isInList = %d", productsSet,1];
[fetchRequest setPredicate:predicate];
int totalProductsInList = [context countForFetchRequest:fetchRequest error:error];
如果我注释int countProducts = productsSet.count;我有这些错误:
-[NSNull unsignedIntValue]: unrecognized selector sent to instance 0x22b9cd8
2011-12-05 12:10:41.418 xxxxxxxxxx[32964:1bb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[NSNull unsignedIntValue]: unrecognized selector sent to instance 0x22b9cd8'
isInList是一个Int16
谢谢,
编辑:
如果我在nspredate中移动1,没有计数,我得到相同的错误:
[NSPredicate predicateWithFormat: @"self IN %@ AND isInList = 1", productsSet;
编辑2:
因为我找不到它不工作的原因,我检查了productsSet。计数,如果它大于0,我执行NSFetchrequest,问题解决了
创建NSSet之后,我检查NSSet。count,如果它大于0,我会发出NSFetchrequest,如果它是0,不要。
问题解决了,但我仍然对这个奇怪的错误感到好奇,如果我不使用nsset.count.
谢谢,
relationship返回的NSSet对象不是普通的NSSet对象,而是_NSFaultingMutableSet类的对象。在nsprecate中使用它似乎不安全。可能会变成故障状态或其他什么。问题的原因看起来与Core Data/NSOperation:在枚举和删除对象时崩溃相同。
所以最好的方法是使用result of allObjects或者创建一个新的NSSet对象: NSArray *productsArray = [currentList.ingredients allObjects];
或
NSSet *productsSet = [NSSet setWithSet:currentList.ingredients];