任何人都可以建议一种更快的方法来解决以下问题:
我有一个包含 5,000 个托管对象的数组(有故障)(car.h 对象的数组)
每个对象都有一组项目 (toCarParts.h)。该集合可以有任意数量的对象。
现在我想根据搜索查询 carpart 数组中最匹配的内容对这些进行排序。
我搜索车轮、座椅、 window 、镜子。
该方法将遍历每辆车并找到最接近的匹配,并计算百分比。因此,如果汽车 A 有车轮、座椅、车窗、镜子、垫子、轮胎、雨刷、管道 --> % 应为 50%。 (匹配 4/8 零件。
这很简单,但问题是搜索 5,000 个项目需要很长时间(即使使用 coredata)。
我使用的逻辑类似于:(伪代码)
For each Car*car in array.
NSMutableArray *x=[car tocarparts]allobjects];
For the count of objects in x.
Carpart*part=objectatindex...i.
If the name of this matches one of my parts
add a count to my counter.
At the end of the loop counter/[x count] =%.car.percent=%.
必须有更好的方法,有什么建议吗? (我认为划分和检查每个部分会花费很长时间。
提前谢谢您。
已编辑,添加以下代码:.
-(NSMutableArray*)calculatePercentagePerFind:(NSMutableArray*)CarArray:(NSMutableArray*)partsArray{ NSArray*defaultParts =[NSArray arrayWithArray:[[[HelperMethods alloc]init]getObjectUserDefault:@"AvailableDefaultParts"]];
int lowestPercentMatchInt=[[[HelperMethods alloc]init]getIntegerUserDefault:@"lowestPercentageMatch"];
NSMutableArray*partsFromCarArray=[[NSMutableArray alloc]init];
NSMutableArray*returnArray=[[NSMutableArray alloc]init];
NSMutableArray *partsWithDefaultParts =[NSMutableArray arrayWithArray:partsArray];
[partsWithDefaultParts addObjectsFromArray:defaultParts];
for (int i=0; i<[CarArray count]; i++) {
double matchCount=0;
Car *CarResult =(Car*)[CarArray objectAtIndex:i];
//Check if it will at least be 30% match
double number1 = [partsWithDefaultParts count];
number1 =(number1/[CarResult.numberOfParts doubleValue])*100;
if (number1>lowestPercentMatchInt) {
partsFromCarArray =[NSMutableArray arrayWithArray:[[CarResult toParts]allObjects]];
NSMutableArray *faultedParts=[[NSMutableArray alloc]init];
for (int i =0; i<[partsFromCarArray count]; i++) {
CarPart*part = (CarPart*)[partsFromCarArray objectAtIndex:i];
[faultedParts addObject:part.name];
}
// for each part in the Car
for (NSString *partInCar in partsWithDefaultParts){
//if the search parts contain that part, add one to count
if ([faultedParts containsObject:partInCar]) {
matchCount++;
}
}
//Calculate percent match
double percentMatch = matchCount;
percentMatch =(percentMatch/[CarResult.numberOfParts doubleValue])*100;
//if at least 30%(user default) then add the percent match to Car result
if (percentMatch >lowestPercentMatchInt) {
if (percentMatch>100) {
CarResult.percentMatch = [NSNumber numberWithDouble:100.00];
}else{
CarResult.percentMatch = [NSNumber numberWithDouble:percentMatch];
}
[returnArray addObject:CarResult];
}
}
}
NSLog(@"Percent Matched Cars = %i",[returnArray count]);
return [self arrangeByHighestPercentMatch:returnArray];
}
最佳答案
试试这个,我相信这将最大限度地减少核心数据的压力。
NSSet *selectionSet; // contains the selected parts
NSPredicate *filter = [NSPredicate predicateWithFormat:
@"self IN %@", selectionSet];
float percentageSum = 0;
NSSet *parts;
for (Car *car in fetchedObjects) {
parts = car.parts; // so the relationship is retrieved only once
percentageSum +=
[parts filteredSetUsingPredicate:predicate].count*1.0f
/ (parts.count*1.0f);
}
return percentageSum/fetchedObjects.count;
这将平均所有汽车的百分比。还有其他方法可以对总体中的各部分进行不同的称重。
您的问题尚不清楚,但如果您不需要总百分比,而是每辆车一个百分比,则无需遍历所有汽车 - 您可以在显示时即时计算百分比(例如具有 transient 属性)。
关于ios - 计算 xcode 中 1,000 个对象缓慢的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14350477/