I'm having a problem fetching results from a Core Data store using predicates. What my application does is fetches results from the store based on one or more predicates and shows you either the results based on all of the predicates or the results based on each predicate.
It works fine for the most part, except I cannot get 'less than' queries to work. Here is the code I am using:
case CriteriaSelectionIsLessThan:
pred = [NSPredicate predicateWithFormat:@"ANY value.attribute.name == %@ AND ANY value.%@ < %@", [attribute name], keyPath, [[activeValue value] value]];
break;
case CriteriaSelectionIsMoreThan:
pred = [NSPredicate predicateWithFormat:@"ANY value.attribute.name == %@ AND ANY value.%@ > %@", [attribute name], keyPath, [[activeValue value] value]];
break;
The code for the 'more than' predicate works fine, it returns the data set for the query, such as > 1970 (a year). When I try to filter it using the 'less than' predicate, such as < 1970, it returns the entire Core Data dataset (unfiltered).
[[activeValue value] value] is an NSNumber.
I can't figure it out for the life of me - the predicates are exactly the same but with one character differing!
Help would be greatly appreciated. If you require any more code/information let me know.
Edit: here's how I map the data type on import from JSON:
[attrib setDataType:[NSNumber numberWithInteger:[[attribute valueForKey:@"type"] integerValue]]];...
// Set the correct data type of the value
switch ([[attrib dataType] integerValue]) {
case AttributeDataTypeNumber:
[value setNumberValue:[NSNumber numberWithInteger:[valueForKey integerValue]]];
break;
case AttributeDataTypeString:
[value setStringValue:(NSString *)valueForKey];
break;
case AttributeDataTypeDate: {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-MM-YYYY"];
[value setDateValue:[df dateFromString:(NSString *)valueForKey]];
}
break;
case AttributeDataTypeBool:
[value setBooleanValue:[NSNumber numberWithBool:[valueForKey boolValue]]];
break;
}
See, the data type in my JSON is guaranteed to be right as I check that before importing it (not in the app). Even when I build a predicate that explicitly says that I want a certain dataType (such as value.attribute.dataType == 1 with 1 being a number in a enum) it still doesn't work. It's really bizzarre.