1

First time building an NSPredicate.

I would like to search a managedobjectcontext using this logic:

Search for a, grab all matches
Search for b, grab all matches, etc....

Nsarray *results = (has all a results, b results, etc);

My attempted predicate is:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name== %@ OR name == %@ OR name == %@",a,b,c];

However I get errors with this predicate...

Edited: Sample method I wrote

-(NSPredicate*)parsePartsIntoAPredicate:(NSMutableArray*)inputPartsNames{
    NSSet *keys=[NSSet setWithArray:inputPartsNames];
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"any self.@name in %@", keys];
    NSLog(@"predicate = %@",predicate);
    return predicate;
}

Clarify: I have a database of cars (20,000) Each car has multiple parts. I want to find all cars that have part a, and all cars that have part b, and all that have part c. Then I want to return an array with cars with part a, b, c, etc...

If you think there is a better way let me know, but I am approaching this backwards. I am saying

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cars" inManagedObjectContext:[self managedObjectContext]];
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:[self parsePartsIntoAPredicate:inputParts]];

    NSError *error;
    NSArray *records = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];

What am I doing wrong?

William Falcon
  • 9,813
  • 14
  • 67
  • 110
  • 2
    What error do you get? May be it's typo but"p" in "NSpredicate" should be capital. – Rahul Wakade Jan 02 '13 at 08:06
  • 1
    try to make it more clear,helping without much information is bit risky! – DD_ Jan 02 '13 at 09:30
  • To get helpful answers you have to show at least: 1) The definition of the managed object entity, 2) the exact predicate and fetch request (copy/paste without typos!), 3) the exact error message. – Martin R Jan 02 '13 at 12:33
  • Sorry did it on iphone, will post from comp – William Falcon Jan 02 '13 at 13:17
  • 1
    What error do you get? Is `name` an attribute of the `Cars` entity? In that case try `[NSPredicate predicateWithFormat:@"name IN %@", keys]`. – Martin R Jan 02 '13 at 14:10
  • [NSPredicate predicateWithFormat:@"ANY name IN %@", keys] (looks like it works, thanks), post as answer – William Falcon Jan 02 '13 at 14:22
  • Your question is not at all clear, have a look at this question and see the first answer [NSPredicate 'OR' filtering based on an NSArray of keys](http://stackoverflow.com/questions/11115377/nspredicate-or-filtering-based-on-an-nsarray-of-keys) and try to use it in your code – DD_ Jan 02 '13 at 07:15

3 Answers3

2
NSString *key;
NSMutableArray *tempArray;
NSPredicate *searchForName = [NSPredicate predicateWithFormat:@"name = %@", key];
NSArray *filterArray = [tempArray filteredArrayUsingPredicate:searchForName];
NSLog(@"%@",filterArray);

Where key is your searchKeyword, tempArray is your CompleteArray in which data is present.

Use Like this. Please put your data.

Manu
  • 4,730
  • 2
  • 20
  • 45
  • is it faster to sort an existing array, or to put the predicate in a fetch request? So in essence, would I ask coredata for ALL cars, then one by one find the cars that match each part? – William Falcon Jan 02 '13 at 13:53
2

Use this

NSPredicate* predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ '%@'", @"SELF contains[c] ",searchText]];
Vishal
  • 556
  • 4
  • 18
  • inputs have to be mutable... it could be 2 variables, could be 100. Also would this do an OR? So find all that have a, find all that have b, find all that have c, etc... – William Falcon Jan 02 '13 at 13:51
0

To fetch all Cars objects that have a name which is one of the strings in the keys set or array, use

[NSPredicate predicateWithFormat:@"name IN %@", keys]
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382