Hi,
Take a quick look at the nsfetchedresultscontroller docs and you will see that the fetchRequest property can't be changed dynamically.
You will have to recreate the entire nsfetchedresultscontroller. What I would do is create it lazily. That way when your change your sort order, you set self.fetchedresultsController =nil
Then when the fetched results controller is called by y our tableView, it gets recreated with the new sort order.
do a search on my other posts and you will find one that talks about lazily creating hour nsfetchedresultscontrollerresults controller and nsfetchedresultscontroller nsfetchrequest.
Per apple's docs, don't forget to delete the cache if you change the nsfetchedresultscontroller!
Good luck
T
Quote:
Originally Posted by hstaniloff
I'm trying to change the sort of fetchedresultscontroller. Is this possible to do or do I have create another FRC with a different sortDescriptor?
Here's my code, but it doesn't work - only shows a blank table when inserted in the viewWillLoad callback. Any ideas how to do this? Thanks!
Code:
[self getAppSettingSortOrder];//Sets currentSortOrder for the class.
if (sortOrderChanged) {
//Form a new fetchrequest for the FRC and reload the table.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Aircraft" inManagedObjectContext:managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:currentSortOrder ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
[fetchedResultsController performFetch:&error];
[self.tableView reloadData];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
}
|