Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7177565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:44:26+00:00 2026-05-28T16:44:26+00:00

Very strange results using NSPredicate combined with a search box on a tableview and

  • 0

Very strange results using NSPredicate combined with a search box on a tableview and NSFetchedResultsController

It’s supposed to be filtering based on matching name… however, if I type in any valid name, I get a few results that are just not correct. If I type in any full name like “Mike Johnson” or “Kelly Michaels” I always get the same filtered result of “Angelo Smith”.

The NSLog will show
“name CONTAINS[cd] \”Kelly Michaels\””

Yet the filtered result displaying on screen will only show Angelo Smith? Any ideas how to troubleshoot this?

I’ve been using the solution in this stack post if you need more details of what I’m doing
How to filter NSFetchedResultsController (CoreData) with UISearchDisplayController/UISearchBar

- (NSFetchedResultsController *)newFetchedResultsControllerWithSearch:(NSString *)searchString
{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

NSPredicate *filterPredicate = nil;

/*
 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:@"Staff" inManagedObjectContext:self.managedObjectContext];

[fetchRequest setEntity:entity];

NSMutableArray *predicateArray = [NSMutableArray array];

if(searchString.length)
{
    NSLog(@"here searchString is %@", searchString);

    // your search predicate(s) are added to this array
    [predicateArray addObject:[NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", searchString]];
    // finally add the filter predicate for this view

    NSLog(@"%@", predicateArray);

    if(filterPredicate)
    {
        filterPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:filterPredicate, [NSCompoundPredicate orPredicateWithSubpredicates:predicateArray], nil]];
    }
    else
    {
        filterPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicateArray];
    }
}


   [fetchRequest setPredicate:filterPredicate];
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:44:28+00:00Added an answer on May 28, 2026 at 4:44 pm

    I wouldn’t create a new FRC when filtering unless the dataset is changing. Instead just change the predicate and do another fetch (in a custom method called something like filterContentForSearchText)

    Example:

    -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {   
        [self filterContentForSearchText:searchText];   
    }
    
    -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
    {
        searchBar.text = @"";
        NSPredicate *predicate = nil;
        [self.fetchedResultsController.fetchRequest setPredicate:predicate];
        [self.fetchedResultsController.fetchRequest setFetchLimit:0];  // 0 is no limit
    
        [searchBar resignFirstResponder];
        NSError *error = nil;
        if (![[self fetchedResultsController] performFetch:&error]) {
            // Handle error
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  //
        }
        [self.myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    
    }
    
    - (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];  // I use 100 because of the large dataset I am working with (4500 records)
        }
    
        NSError *error = nil;
        if (![[self fetchedResultsController] performFetch:&error]) {
            // Handle error
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  // Fail
        }  
        [self.myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    }
    
    -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
        [self filterContentForSearchText:searchBar.text];
        [searchBar resignFirstResponder];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've encountered a very strange issue using JMenuBar where navigating between two JMenus results
Using ui:repeat for simple listing of elements produces very strange results - when you
I have a very strange problem when I using ajax in symfony 1.4. I've
This is very very strange. Basically, i'm using ASP.NET. What I want is to
I have a very strange problem. Under some elusive circumstances I fail to apply
I have a very strange problem, when I try to var_dump (or print_r )
I have a very strange problem with the Delphi 2006 IDE. If the IDE
We have very strange problem, one of our applications is continually querying server by
I find this very strange, must be something I'm doing wrong, but still... I'm
I am having a very strange behavior in JBoss, and I'd like avail myself

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.