dynamically change sort descriptor for Fetched results controller
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?
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.
精彩评论