This one is baffling me:
I have a core data set that I search/filter using the following code:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Lead" inManagedObjectContext:moc]];
NSString *predicateString = [NSString stringWithFormat:@"("
"(isLocalVersion == %@) AND (LeadStatusID =='Active')"
") AND ("
"(LeadID contains[cd] '%@')"
"OR (AccountNumber contains[cd] '%@')"
"OR (ANY contactItems.FirstName contains[cd] '%@')"
"OR (ANY contactItems.LastName contains[cd] '%@')"
"OR (ANY addressItems.Address1 contains[cd] '%@')"
"OR (ANY addressItems.City contains[cd] '%@')"
")", [NSNumber numberWithBool:YES], sTerm, sTerm, sTerm, sTerm, sTerm, sTerm];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *leads = nil;
@try {
leads = [moc executeFetchRequest:fetchRequest error:&error];
}
@catch (NSException *exception) {
LogSevere(@"Error Executing Fetch Request: %@", exception);
leads = [NSArray array];
}
@finally {
[fetchRequest release];
}
sTerm is just a NSString
.
This works 95% of the time, however every once in a while it catches an NSInvalidArgumentException: Can't use in/contains operator with collection 10076173 (not a collection).
The Predicate String formats to be this:
((isLocalVersion == 1) AND (LeadStatusID =='Active')) AND ((LeadID contains[cd] 'i')OR (AccountNumber contains[cd] 'i')OR (ANY contactItems.FirstName contains[cd] 'i')OR (ANY contactItems.LastName contains[cd] 'i')OR (ANY addressItems.Address1 contains[cd] 'i')OR (ANY addressItems.City contains[cd] 'i'))
My catch allows me to not crash and just return 0 results, but this isn't optimal. Has anyone ever seen this before?
My only clue is that it seems to happen after I modify (and save) a record in the core data (again only sometimes).
Yep, you can't have expressions that evaluate to collections as part of a fetchRequest's predicate. This is in the documentation:
Aggregate expressions are not supported by Core Data.