why is this 'else if' block getting executed twice inspite of not being in a loop?!

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];
    }
Answered by ace.neerav in 45795022

i found out the silly mistake. the method inside which this code was, was being called twice inside viewDidAppear!

Have you added NSLog() statements to log your progress and values to the debug output as you go through the code?

It's very unlikely that a particular if/else branch is getting executed more than once.


It's more likely that this line isn't catching what you expect:

if ([_sortDictionary valueForKey:@"_locationVenues"]!=[NSNull null])


This will still evaluate as true if there isn't anything set for the key @"_locationVenues" and the request returns nil, or if the object retrieved is an empty array.


Neither of those cases is handled in that branch, because venues.count will be 0 for either, and your code only handles

if (venues.count>1)

and

else if (venues.count==1)

which would make locationsPredicate nil since it never gets set.

Accepted Answer

i found out the silly mistake. the method inside which this code was, was being called twice inside viewDidAppear!

why is this 'else if' block getting executed twice inspite of not being in a loop?!
 
 
Q