views:

237

answers:

1

I have a tableview with a search display controller. It has been working fine in the past, but recently has started crashing for certain search results. Here my code searches a Golfer based on their Name, Age and Handicap. The data is correctly loaded into the table, I can access and drill down to receive further information. However when I type in a search query for either Name or Age, the app crashes, while the Golfers Handicap is returned fine.

Note: dataSouceArray is the data source for the tableview, dataSourceArrayCopy is the mutable copy of the data used to add and remove objects in the search filter.

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
    /*
     Update the filtered array based on the search text and scope.
     */

    [self.dataSourceArrayCopy removeAllObjects]; // First clear the filtered array.

    /*
     Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
     */
    for (Golfer *golfer in dataSourceArray){
        if ([scope isEqualToString:@"Name"] || [golfer.golferName isEqualToString:scope]){
            NSComparisonResult result = [golfer.golferName compare:searchText 
                                                           options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) 
                                                             range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame){
                [self.customerListCopy addObject:golfer];
            }
        }
        if ([scope isEqualToString:@"Age"] || [golfer.golferAge isEqualToString:scope]){
            NSComparisonResult result = [golfer.golferAge compare:searchText 
                                                          options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) 
                                                            range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame){
                [self.dataSourceArrayCopy addObject:golfer];
            }
        }
        if ([scope isEqualToString:@"Handicap"] || [golfer.golferHandicap isEqualToString:scope])
        {
            NSComparisonResult result = [golfer.golferHandicap compare:searchText 
                                                               options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) 
                                                                 range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame)
            {
                [self.dataSourceArrayCopy addObject:golfer];
            }
        }
    }
}  
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

Any help would be appreciated, thank you for taking the time to read this.

A: 

Oddly enough when testing the exact same code on another developers phone, it didn't crash the program. Not much of an answer, but let this be a lesson to everyone, test on multiple devices.

Convolution