Question: I have a search bar with my table view, but I am not using a `UISearchDisplayController` to display the results, instead I'm using my table view.

The code I have appears to work, but I wonder if it is the acceptable thing to do. When the user starts typing, I add a predicate to to perform the search, when the user deletes the text inside the search bar, I nil out the `NSFetchResultsController` to fetch everything.

Am I doing anything horribly wrong here, or is there a better way to do this?

```
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([searchText length] == 0) {
        _fetchedResultsController = nil;
    }
    else {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", searchText];
        [[_fetchedResultsController fetchRequest] setPredicate:predicate];
    }

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }

    [[self tableView] reloadData];
}

```

Answer: 
The only thing different I do is add a fetch limit to the results in the event you have a lot of records, but yes, there is nothing wrong with this method. 

Maybe something like

```
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", searchText];
[[_fetchedResultsController fetchRequest] setPredicate:predicate];
[[_fetchedResultsController fetchRequest] setFetchLimit:50];

```
A clean way is to create a mutable array that will contain only the objects that match with your search.

```
self.searchResults = [NSMutableArray array];

```

Since you don't use a `UISearchDisplayController` you need a boolean flag that indicating that you are performing the search or not.

```
self.performSearch = NO;

```

When you perform the search, do like the following.

```
- (void)performSearchForTerm:(NSString *)term {
    self.performSearch = YES; // you need to reset after!!!

    [self.searchResults removeAllObjects];

    NSFetchRequest *fetchRequest = // create your request here...

    NSError *error = nil;
    NSArray *results = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
    if (error) {
        // do something here...
    }

    [self.searchResults addObjectsFromArray:results];
}

```

Now, you need to switch from `self.fetchedResultsController` to `self.searchResults` and vice versa based on `self.performSearch`.

The idea has been taken from [Searching in `UITableView`](https://github.com/subdigital/nsscreencast/tree/master/036-searching-in-uitableview) by **@BenSheirman**. Then I've modified it according to your requirements.

Hope it helps.