如何预取NSManagedObject关系的关系

时间:2013-01-08 15:59:42

标签: ios core-data nsmanagedobject nsfetchrequest

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

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

NSMagagedObject Parts.h (零件有一个叫做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];
}

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

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"];

提前谢谢!

2 个答案:

答案 0 :(得分:0)

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Post"];
[fetchRequest setRelationshipKeyPathsForPrefetching:@[@"Picture"]];

答案 1 :(得分:-2)

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