views:

206

answers:

2

Is there a way to have the sort descriptor(s) be dynamically set for a fetched results controller on iOS?

For instance, I need to have the core data results returned to me sorted based on the setting of a segmented control in the navigation title bar. The user can either click the segmented control to sort by Price or Priority.

I then need the fetched results controller to return and display the core data info sorted into sections based on that segmented control value.

I know how to set the sort descriptors initially, but I'm uncertain how to change it/update it dynamically. Since the - (NSFetchedResultsController *)fetchedResultsController function is lazy loaded, wont it always just return the frc that was created the first time through (thus returning the initial sort descriptor setting)?

Would I do something like store the segmented control value in a the sharedPreferences, and then when it is changed, set my fetchedResultsController to nil so that a new one will be generated? Then within the fetchedResultsController function I can create my sortDescriptor based on that sharedPreferences setting?

A: 

You are in control of that. You could, for example, release the controller when the segmented control is updated and call reload on your table. This would cause the controller to be rebuilt with the new sort.

There are other solutions but they depend on your app design.

Marcus S. Zarra
ahh Marcus, your feed tutorial in PragProg is one of the first articles I read on NSFetchedController, so thanks for the response and that article.So, its easy enough to just release my fetched controller and set it to nil, and allow the ViewController to regenerate it based on the segmented control. Sounds easy enough. Thanks.
cpjolicoeur
@Marcus, is it just a matter of calling `[self.tableView reloadData]` when using a fetched results controller, or is there another method required to reload data via a FRC?
cpjolicoeur
Once you have called `-performFetch:` on the `NSFetchedResultsController` you can call `-reloadData` on the table. Nothing else is required.
Marcus S. Zarra
A: 

I'm having the same issue and having difficulty understanding how to update my frc. I initialize a frc the standard way. But how exactly do you modify the fetchrequest with a new sortdecripto, perform the fetch on the frc and reload the table? Here is my code that is not working (just a blank table). Any ideas??? Thanks!

[self appSettingSortOrder];     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];      
}
Hal