我正在做一個搜索,需要加快一點。我有一系列汽車零件,我想找到有這些零件的汽車。這部分我很好。 NSManagedObject Cars.h如何預取關係的關係NSManagedObject
(汽車有一個叫NSSet中ToCarParts,其中列出了所有的汽車的汽車零部件)
NSMagagedObject Parts.h (零件有一個叫NSSet中ToCars,其中列出了相關的所有汽車與此)
//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"];
預先感謝您!
,而不是否決也許一個更好的答案應該給... –