views:

44

answers:

0

Something strange is going on with my iphone app. I am using Core Data to store data in a SQLite database. The first time after my app starts up I try to read a table to return all of the rows to fill a UITableView with a list of things for the user to select from. If they chose the top item in the list I get a EXC_BAD_ACCESS exception. If they choose any other item everything seems OK. Here is the code sample: Sport and Team are NSManagedObjects. sports is an NSArray of Sport objects fetched using an NSFetchedResultsController (fetchedObjects). I can display the list of objects fine in the UITableView (I use the same array for the cellForRowAtIndexPath() call without any problem

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Sport *sport = (Sport *)[sports objectAtIndex:indexPath.row];

    if (teamSetup) {
        if (team.sport != sport) {
            [team setSport:sport]; <-- this is where the EXC_BAD_ACCESS happens

            NSError *error;
            [team.managedObjectContext save:&error];
        }
        [self.navigationController popViewControllerAnimated:YES];

    } else {
        // .. do some other stuff

    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

UPDATE: It seems to only affect the last record in the NSArray instead of the first until it throws the EXC_BAD_ACCESS error. After I restart the app again the problem goes away and the code works as expected :(