Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use NSFetchedResultsController and UISearchDisplayController
    text
    copied!<p>I've been creating an iPhone App using Core Data.</p> <p>First of all, does it make sense to use an NSFetchedResultsController and a UISearchDisplayController together to fetch the result? Would you recommend something else?</p> <p>I've been trying quite long combining an NSFetchedResultController and a UISearchDisplayController. I've been thinking of setting an NSPredicate in the <code>(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption</code> method to the NSFetchedResultController of the UIViewController. But this isn't working that well.</p> <p>So, have you any idea how to implement a solution for my problem? Thank you already for posting answers or links to good tutorials.</p> <p>EDIT</p> <p>Here is my code. The UISearchDisplayDelegate methods call the method <code>(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope</code> that should set the predicate in the NSFetchedResultController. I've also added the code of the NSFetchedResultController.</p> <pre><code>- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterContentForSearchText:searchString scope: [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; // Return YES to cause the search result table view to be reloaded. return YES; } - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope: [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; // Return YES to cause the search result table view to be reloaded. return YES; } - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSString *query = self.searchDisplayController.searchBar.text; if (query &amp;&amp; query.length) { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name contains[cd] %@", query]; [fetchedResultsController.fetchRequest setPredicate:predicate]; } NSError *error = nil; if (![[self fetchedResultsController] performFetch:&amp;error]) { // Handle error NSLog(@"Unresolved error %@, %@", error, [error userInfo]); exit(-1); // Fail } } - (NSFetchedResultsController *)fetchedResultsController { if (fetchedResultsController != nil) { return fetchedResultsController; } /* Set up the fetched results controller. */ // Create the fetch request for the entity. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; // Edit the entity name as appropriate. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]; [fetchRequest setEntity:entity]; // Set the batch size to a suitable number. [fetchRequest setFetchBatchSize:20]; // Edit the sort key as appropriate. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; // Edit the section name key path and cache name if appropriate. // nil for section name key path means "no sections". NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"]; aFetchedResultsController.delegate = self; self.fetchedResultsController = aFetchedResultsController; [aFetchedResultsController release]; [fetchRequest release]; [sortDescriptor release]; [sortDescriptors release]; return fetchedResultsController; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload