Search code examples
iphonecore-datanspredicatensset

NSSet in NSPredicate, only works if first i issue a nsset.count


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.


Solution

  • 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,