使用NSPredicate OR语句进行搜索的最有效方法 [英] most effective way of searching using NSPredicate OR statement
问题描述
我试图在上一篇文章中建立一个谓词,但最终这样做是因为3个小时后,我无法正常工作。
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辆车
我有轮胎,车轮,作为三个汽车零件的座位
我想找到所有有轮胎的汽车。
然后,我想找到所有有轮毂的汽车。
然后我想找到所有有座位的汽车。
(我知道很多重复项,但这就是我所需要的)。
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.
谢谢!
-(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;
}
推荐答案
听起来像你真正要寻找的是如何根据可能的匹配项数组构造谓词。在这种情况下,您可以执行以下操作:
It sounds like what you're really looking for is how to construct a predicate based on an array of possible matches. In that case you'd do something like this:
NSMutableArray *partPredicates = [NSMutableArray arrayWithCapacity:[inputCarParts count]];
for (NSString *partName in inputCarParts) {
NSPredicate *currentPartPredicate = [NSPredicate predicateWithFormat:@"partName =[c] %@", partName];
[partPredicates addObject:currentPartPredicate];
}
NSPredicate *fullPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:partPredicates];
然后得出的谓词类似于 partName == [c]轮胎或部件名称== [c]车轮或部件名称== [c]座椅
,每个数组条目具有一个组件。一次提取即可一次评估它们。
The resulting predicate would then be something like partName ==[c] "tire" OR partName ==[c] "wheel" OR partName ==[c] "seat"
, with one component per array entry. Do one fetch to evaluate them all in one shot.
如果在提取中使用它,将不会得到重复的结果。您对这个问题发表了评论,表明您不想重复。如果是这种情况,那么我不确定我知道很多重复的东西,但这就是我需要的。听起来好像您想要骗子,但您却说自己没有。
This will not get duplicate results if you use it in the fetch. You left a comment on the question that indicates you don't want duplicates. If that's the case though, then I'm not sure what I know many will be duplicates, but that's what I need means. It sounds like you wanted the dupes, but you said you didn't.
这篇关于使用NSPredicate OR语句进行搜索的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!