views:

466

answers:

1

Hi All, Not sure if this is the right place (I am sure someone will let me know if it is not) I have a iPhone application that has a UITableview that is backed by core data. I want to perform a reducing search so that only the items starting with the characters entered into the search bar are shown. This is normally done with the delegate - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText no problem. I am a little confused as I am new to Core Data how to do this. One of the big problems as I see it is going to be updating the interface to let it know what to present. I assume an alternative NSFetchedResultsController needs to be sent to the UITableView is that correct?

So here are my issues: 1) I assume I need to create a NSFetchedResultsController with only the correct items in it then tell the UITableView to use this as the dataSource and reload the table? 2) is there a better way than executing a full sorted fetch and removing those objects that do not conform. ie is there a way of doing a select where type fetch?

Thanks in advance and sorry if this is a dumb question.

Regards Damien

+1  A: 
  1. Yes, you will need a new NSFetchedResultsController.
  2. You should use a NSPredicate in your new NSFetchRequest to filter by your search text.

For example, if your managed objects have a field "name" that should be filtered:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K beginswith[c] %@", @"name", searchText];
[fetchRequest setPredicate:pred];
gerry3