I am doing a search and need to speed things up a bit. I have an array of car parts, and I want to find cars that have those parts. This part Im doing ok.
NSManagedObject Cars.h
(cars has an NSSet called ToCarParts, which lists all the car parts for the car)
NSMagagedObject Parts.h
(Parts has an NSSet called ToCars, which lists all the cars associated with this)
//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];
}
The issue comes later when I take the result from here, and have to load up all the carParts later in the app.
Car *carResult =(Cars*)[resultsConvertedToCarsArray objectAtIndex:i];
partsFromCar =[NSMutableArray arrayWithArray:[[carResult toParts]allObjects]];
How do I prefetch the above in my original query so that this information is already loaded in the object?
Here is my original fetch request:
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"];
Thank you in advance!
Created an nsmanagedobject to hold these objects…