I have this big piece of code which makes core data predicates based in user selection of sort criteria. the user either selects all categories, or all venues of one category or one/many venues of one category.
when i select 1 venue of 1 category, the last else if (venues.count==1) is supposed to be executed. for some unknown reason it is executed twice! this makes the predicate nil the second time!
i have been beating my head about it for sometime now and have checked for any accidental braces for loops etc.. nothing seems to be wrong. i know somewhere there is a silly mistake on my part. if u can please help me spot it out.
thanks...
//predicate for location
NSPredicate *locationsPredicate;
if ([_sortDictionary valueForKey:@"_locationCategories"]!=[NSNull null])
{
NSExpression *categoryLHS, *categoryRHS;
NSArray *categories = [_sortDictionary valueForKey:@"_locationCategories"];
categoryLHS = [NSExpression expressionForKeyPath:@"location.category"];
if (categories.count>1)
{
NSMutableArray *categoriesComparisionPredicates = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *category in categories)
{
categoryRHS = [NSExpression expressionForConstantValue:category];
NSPredicate *categoryEqualToPredicate = [NSComparisonPredicate predicateWithLeftExpression:categoryLHS rightExpression:categoryRHS modifier:NSDirectPredicateModifier type:NSEqualToPredicateOperatorType options:0];
[categoriesComparisionPredicates addObject:categoryEqualToPredicate];
}
locationsPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:categoriesComparisionPredicates];
}
else if (categories.count==1)
{
categoryRHS = [NSExpression expressionForConstantValue:categories[0]];
NSPredicate *categoryEqualToPredicate = [NSComparisonPredicate predicateWithLeftExpression:categoryLHS rightExpression:categoryRHS modifier:NSDirectPredicateModifier type:NSEqualToPredicateOperatorType options:0];
if ([_sortDictionary valueForKey:@"_locationVenues"]!=[NSNull null])
{
NSExpression *venueLHS, *venueRHS;
NSPredicate *venuesPredicate;
NSArray *venues = [_sortDictionary valueForKey:@"_locationVenues"];
venueLHS = [NSExpression expressionForKeyPath:@"location.venueID"];
if (venues.count>1)
{
NSMutableArray *venuesComparisionPredicates = [[NSMutableArray alloc] initWithCapacity:0];
for (Location *venue in venues)
{
venueRHS = [NSExpression expressionForConstantValue:venue.venueID];
NSPredicate *venueEqualToPredicate = [NSComparisonPredicate predicateWithLeftExpression:venueLHS rightExpression:venueRHS modifier:NSDirectPredicateModifier type:NSEqualToPredicateOperatorType options:0];
[venuesComparisionPredicates addObject:venueEqualToPredicate];
}
venuesPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:venuesComparisionPredicates];
locationsPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[categoryEqualToPredicate,venuesPredicate]];
}
//the following else if block gets executed twice for no reason!
else if (venues.count==1)
{
venueRHS = [NSExpression expressionForConstantValue:((Location*)venues[0]).venueID];
NSPredicate *venueEqualToPredicate = [NSComparisonPredicate predicateWithLeftExpression:venueLHS rightExpression:venueRHS modifier:NSDirectPredicateModifier type:NSEqualToPredicateOperatorType options:0];
locationsPredicate = venueEqualToPredicate;
}
// as a result, location predicate becomes nil the second time!
}
else
{
locationsPredicate = categoryEqualToPredicate;
}
}
NSLog(@"location predicate:\n%@",locationsPredicate);
[predicatesArray addObject:locationsPredicate];
}