1

我正在搜索,需要加快速度。我有一系列汽车零件,我想找到有这些零件的汽车。这部分我做得很好。NSManagedObject Cars.h

(汽车有一个名为 ToCarParts 的 NSSet,它列出了汽车的所有汽车零件)

NSMagagedObject Parts.h(Parts 有一个名为 ToCars 的 NSSet,它列出了与此关联的所有汽车)

//This takes all results gathered by using parts to search for car, and converts them to have the main object be a car
-(NSArray*)convertResultsToCars:(NSArray*)records{
    NSMutableArray *manipulator =[NSMutableArray arrayWithArray:records];
    NSMutableArray *convertedArray =[[NSMutableArray alloc]init];

    for (int i=0; i<[manipulator count]; i++) {
        CarPart *FromManipulator=(CarPart*)[manipulator objectAtIndex:i];
        [convertedArray addObjectsFromArray:[[partFromManipulator toCars]allObjects]];
    }

    NSLog(@"Results from core data = %i",[convertedArray count]);
    return [NSArray arrayWithArray:convertedArray];
}

当我从这里获取结果时,问题出现了,稍后必须在应用程序中加载所有 carParts。

Car *carResult =(Cars*)[resultsConvertedToCarsArray objectAtIndex:i];

        partsFromCar =[NSMutableArray arrayWithArray:[[carResult toParts]allObjects]];

如何在我的原始查询中预取上述内容,以便此信息已加载到对象中?

这是我的原始获取请求:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"CarParts" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
    [fetchRequest setPredicate:[self parseCarPartsIntoAPredicate:carPartsArray]];
    [fetchRequest setReturnsDistinctResults:YES];
    //I guess I would use something like this? but how would I structure it?
    [fetchRequest setRelationshipKeyPathsForPrefetching:@"I need to prefetch the toParts nsset of the cars found here (although in reality it brings up every instance of that car part, which I then have to convert into a car... something like found a car part, car for that part is a buick, what are the parts of the buick? ok, preload those parts"];

先感谢您!

4

2 回答 2

0
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Post"];
[fetchRequest setRelationshipKeyPathsForPrefetching:@[@"Picture"]];
于 2013-09-15T20:01:39.380 回答
-2

创建了一个 nsmanagedobject 来保存这些对象...

于 2013-01-19T18:03:16.170 回答