Filter Core Data results by property IN array

19,464

Try this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"tid IN %@", filterArray];

[request setPredicate:predicate];

Have a look at the Aggregate Operations in the Predicate Programming Guide.

EDIT

Have a look at NSPredicate iPhone 3.2 SDK Core Data “IN clause” NSInvalidArgumentException exception. It's same error you have. The problem was a typo in the column/attribute name. The syntax should be alright, it can't just find tid.

Share:
19,464

Related videos on Youtube

markdorison
Author by

markdorison

Updated on May 26, 2020

Comments

  • markdorison
    markdorison almost 4 years

    I currently have Core Data successfully returning all of the results for a specific entity titled Event:

    NSManagedObjectContext *context = [delegate managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Event" 
                                                         inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];
    
    NSError *error;
    NSArray *fetchResults = [context executeFetchRequest:request error:&error];
    

    One property of the Event entity is a string titled tid. I also have an array filterArray that contains all allowed tid values.

    How can I get my Core Data request to only return events that have a tid property that matches one of the values in filterArray? I believe the answer relates to NSPredicate but I am not familiar enough with it yet to get it to bend to my will.

  • markdorison
    markdorison almost 13 years
    Thanks for your response. I attempted your suggestion but I am getting an 'NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (tid IN {1, 2})'. Any idea on this one?
  • Guy Kahlon
    Guy Kahlon over 6 years
    Can I use NSPredicate *predicate = [NSPredicate predicateWithFormat:@"tid IN[c] %@", filterArray]; for case insensitive?
  • Mateus Forgiarini da Silva
    Mateus Forgiarini da Silva almost 5 years
    @GuyKahlon have you figured out how to make an IN query case insensitive?
  • Nick Weaver
    Nick Weaver almost 5 years
    I guess doing a map on the array and lowercase every entry might be a solution.