Using NSFetchedResultsController for a UISearchBar

I have a UISearhBarController in my application. He is currently searching the online database, but I am changing it to search the underlying database instead. He currently uses this code:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if (self.billingSearchBar == searchBar) { [self.billingSearchController filterResultsUsingString:searchText]; } } - (void)filterResultsUsingString:(NSString *)filterString { self.billingSearchText = filterString; NSArray *billing_codes = [self billingSearch:self.billingSearchText searchType:self.billingSearchCategory]; self.displayedBillingSearch = billing_codes; self.billingSearch = billing_codes; [self.tableView reloadData]; } -(NSMutableArray*)billingSearch:(NSString*)searchString searchType:(NSString*)searchType { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/server/20111115/60b88126/billing_search/", [self getHost]]]; ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease]; [request setPostValue:searchString forKey:@"query"]; [request setPostValue:searchType forKey:@"category"]; [request startSynchronous]; NSError *error = [request error]; NSString *responseString; if (!error) { responseString = [request responseString]; } NSMutableArray *search_results = [[NSMutableArray alloc] init]; NSDictionary *responseDictionary = [responseString JSONValue]; for (id key in [responseDictionary valueForKey:@"search_results"]) { [search_results addObject:key]; } return search_results; } 

So, I already have the database setup in the main data, I just need to connect the search controller / NSFetchedResults to it. Any easy way to do this?

+6
source share
1 answer

The cleanest way to do this is to use two NSFetchedResultsControllers and set the predicate in your search NSFRC as searchText in the UISearchBar. After you decide to use NSFRC, you simply set your self.fetchedResultsController = "search" NSFRC or "NSFRC" by default.

You can also use the same NSFRC and change the predicate, but this is not recommended for this.

See comment below. If everything that changes is a predicate, one FRC is fine.

Here is sample code to do this:

 // First use the Searchbar delegate methods -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { [self filterContentForSearchText:searchText]; } -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [self filterContentForSearchText:searchBar.text]; [searchBar resignFirstResponder]; } // The method to change the predicate of the FRC - (void)filterContentForSearchText:(NSString*)searchText { NSString *query = searchText; if (query && query.length) { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@ or department contains[cd] %@", query, query]; [self.fetchedResultsController.fetchRequest setPredicate:predicate]; [self.fetchedResultsController.fetchRequest setFetchLimit:100]; // Optional, but with large datasets - this helps speed lots } NSError *error = nil; if (![[self fetchedResultsController] performFetch:&error]) { // Handle error exit(-1); } [self.myTableView reloadData]; } 

It should start.

+6
source

Source: https://habr.com/ru/post/903948/


All Articles