NSPredicate 中的 NSSet,仅当我首先发出 nsset.count 时才有效
我有解决问题的方法,但我真的想了解为什么会遇到这个问题以及如何解决它。
我有一个实体 A 与另一个实体 B 相关,并且 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
谢谢,
编辑:
如果我将 1 移到 NSPredicate 内,没有计数,我会得到相同的错误:
[NSPredicate predicateWithFormat: @"self IN %@ AND isInList = 1", productsSet;
编辑 2:
因为我找不到它不起作用的原因,所以我检查对于 productsSet.count ,如果它大于 0,我会发出 NSFetch 请求,问题就解决了。
I have a workaround for my problem, but I really want to understand why I'm having this problem and how to solve it.
I have an entity A related to another entity B, and some of the rows of A have a special mark in one field.
I want to count how many of those A that are related to B, have this special mark.
All works perfectly, if after I create the NSSet I count the set:
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];
If I comment the
int countProducts = productsSet.count;
I have those errors:
-[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 is a Int16
thanks,
EDIT:
If I move the 1 inside the NSPredicate, without a count, I get the same error:
[NSPredicate predicateWithFormat: @"self IN %@ AND isInList = 1", productsSet;
EDIT 2:
As I can't find why it doesn't work, I check for productsSet.count and if it's greater than 0 I make the NSFetchrequest, problem solved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,创建 NSSet 之后,我检查 nsset.count,如果它大于零,我会发出 NSFetchrequest,如果它为零,则不会。
问题解决了,但如果我不使用 nsset.count,我仍然对这个奇怪的错误感到好奇。
谢谢,
Well, after creating the NSSet I check for nsset.count, and in case it's greater than zero I issue the NSFetchrequest, in case it's zero, don't.
Problem solved, but I still have the curiosity for this strange error that I have if I don't use the nsset.count.
thanks,
关系返回的 NSSet 对象不是普通的 NSSet 而是 _NSFaultingMutableSet 类的对象。而且在 NSPredicate 中使用它似乎不安全。可能会变成故障状态什么的。问题的原因看起来与核心数据中的相同/NSOperation:枚举和删除对象时崩溃。
因此,最好的方法是使用 allObjects 的结果或创建一个新的 NSSet 对象:
NSArray *productsArray = [currentList.ingredients allObjects];
或
NSSet *productsSet = [NSSet setWithSet:currentList.ingredients ];
NSSet object returned by relationship is not an ordinary NSSet but an object of _NSFaultingMutableSet class. And it seems it's not safe to use it in NSPredicate. May be it turns into fault state or something. The reason of the problem looks the same as in Core Data/NSOperation: crash while enumerating through and deleting objects.
So the best way is to use result of allObjects or create a new NSSet object:
NSArray *productsArray = [currentList.ingredients allObjects];
or
NSSet *productsSet = [NSSet setWithSet:currentList.ingredients];