views:

63

answers:

2

I have a tableview with a NSFetchedResultsController data source, displaying a list of names from the underlying Core Data SQLite store. I have implemented a search bar. When the first character is entered in the search bar, a fetch request in the following form is executed:

NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"name beginswith[cd] %@", searchText];

However, when the second search character is entered, I would like to filter the fetched objects of the fetchedResultsController, rather than executing another fetch request (implementing another fetch request, similar to the 1 above, results in another trip to the store which I had hoped would not occur as the results of the second fetch would just be a subset of the first). Is there anyway to filter the fetchedResultsController so that another trip to the store is avoided?

+1  A: 

Yes, set up a search "state" and then switch your NSTableViewDatasource to point at an array that is filtered from the -fetchedObjects returned from your NSFetcResultsController.

You can then update the filter on that array as the user types more information in and it will not go back to the store. This will even allow you to filter on the first character entered and avoid even that unnecessary trip to the store.

Marcus S. Zarra
Thanks, I have considered this, but I have a grouped tableview and I am not sure how to deal with numberOfSections and rowsForSection with this approach
Run Loop
+1  A: 

You could always store the results of the first fetch into an array and when text in the search bar changes, filter the contents of the array with another predicate.

Gordon Hughes