0

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.

mongeta
  • 2,301
  • 2
  • 32
  • 42
  • This error is driving me crazy ... The unsignedIntValue points to the 1 that is it on the NSPredicate ? If I put the 1 inside the NSPredicte like this the error is the same: [NSPredicate predicateWithFormat: @"self IN %@ AND isInList = 1", productsSet]; – mongeta Dec 06 '11 at 06:42

2 Answers2

0

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];

Community
  • 1
  • 1
0

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,

mongeta
  • 2,301
  • 2
  • 32
  • 42
  • same issue for me seems like a core data bug. I don't have the set to check, so I can't do the same thing. looking for a better solution… – Corey Floyd Dec 21 '11 at 22:54
  • glad I'm not alone on this :-) If you find a solution, please, post here. thanks! – mongeta Jan 04 '12 at 08:00