Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tdmiller7873

macrumors newbie
Original poster
Sep 13, 2011
1
0
I'm developing a simple navigation-based iPad app. The initial screen shows a TableView with sort buttons in the toolbar at the bottom.

Everything works fine until I try to change the sort order (via the sort buttons) on the fetchRequest and try to reloadData on the tableVIew.

I'm receiving a "EXC_BAD_ACCESS" error message.

Here is the code being called by the sort buttons:


Code:
-(IBAction) toolbarSortOrderChanged: (id)sender 
{

    NSLog(@"toolbarSortOrderChanged");

    // Get the fetch request from the controller and change the sort descriptor
    NSFetchRequest* fetchRequest =  self.fetchedResultsController.fetchRequest;
    
    // Edit the sort key based on which button was pressed
    BOOL ascendingOrder = NO;
    UIBarButtonItem* button = (UIBarButtonItem*) sender;
    if ([button.title compare:@"Ascending"]== NSOrderedSame) 
        ascendingOrder=YES;
    else    
        ascendingOrder=NO;
    
    NSSortDescriptor *sortDescriptor = 
    [[NSSortDescriptor alloc] initWithKey:@"periodStartDate" ascending:ascendingOrder];
    NSArray *sortDescriptors = 
    [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    
    [fetchRequest setSortDescriptors:sortDescriptors];
    [sortDescriptor release];
    [sortDescriptors release];
    
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    [error release];
    [fetchRequest release];
    
    [self.timesheetTableView reloadData];
}

The first time the routine is called is crashes on the last line:

Code:
[self.timesheetTableView reloadData];

When I comment out this call to reloadData and try running this code, the code will execute fine the first time (although not refreshing the data), but the second time I call this routine (via the same sort button), it crashes on the setSortDescriptors line:

Code:
[fetchRequest setSortDescriptors:sortDescriptors];

Yes...I'm still fairly new to iOS and iPad development, but learning quickly. So, any help would be greatly appreciated.

Thanks, T.D.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.