I tried to build a predicate in a previous post, but ended up doing it this way because after 3 hours, I couldn't get it to work.
I feel like there has to be a more effective way, also I need the predicates to be case insensitive.
10,000 cars I have a tire, wheel, seat as three car parts I want to find all cars that have a tire. THEN I want to find all cars that have a wheel. THEN I want to find all cars that have a seat. (I know many will be duplicates, but that's what I need)
Please let me know if there is a more effective way. Also please let me know how to make the predicates case insensitive.
Thank you in advance!
-(NSArray*) loadCarsFromCoreData:(NSMutableArray*)inputCarParts{
NSMutableArray *totalResults=[[NSMutableArray alloc]init];
NSFetchRequest *fetchRequest =[[NSFetchRequest alloc]init];
//To find the cars we are using the car parts
NSEntityDescription *entity = [NSEntityDescription entityForName:@"CarParts" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSError *error;
NSMutableArray *predicates =[self parseCarPartsIntoAPredicateArray:inputCarParts];
for (int i=0; i<[predicates count]; i++) {
[fetchRequest setPredicate:[predicates objectAtIndex:i]];
NSArray *records = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
NSLog(@"results = %i",[records count]);
[totalResults addObjectsFromArray:records];
}
NSLog(@"results = %i",[totalResults count]);
return [NSArray arrayWithArray:totalResults];
}
-(NSMutableArray*)parseCarPartsIntoAPredicateArray:(NSMutableArray*)inputCarParts{
NSMutableArray *returnArray=[[NSMutableArray alloc]init];
for (int i=0; i<[inputCarParts count]; i++) {
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"partName == %@",[inputCarParts objectAtIndex:i]];
[returnArray addObject:predicate];
}
return returnArray;
}