I am wondering how this works, if I create a Car object, and there is a relationship called toParts (that has a set of all car parts for this car), and then I create another car, will it duplicate the toParts set?
add to core data: car a = (toParts = wheel, tire, seat)
add to core data: car b = (toParts = wheel, tire, seat)
CarParts.h (has a name attribute)
will I now have Carparts = wheel , wheel, tire, tire, seat, seat If so do I need the duplicates? is there a way to just point all future cars to the same wheel instance (as opposed to having 1000 of them)?
thanks in advance
edited added code:
This is the code I am using to get the results right now, I was looking to get ris of the duplicates and get an end array of cars NOT carParts (but I want to search by carPart)
-(NSArray*) loadCarsFromCoreDataUsingCarParts:(NSMutableArray*)inputCarParts{
NSLog(@"carParts =%@",inputCarParts);
NSFetchRequest *fetchRequest =[[NSFetchRequest alloc]init];
//To find the Cars we are using the carParts
NSEntityDescription *entity = [NSEntityDescription entityForName:@"CarPart" inManagedObjectContext:[self managedObjectContext]];
//sets up fetch request details
[fetchRequest setEntity:entity];
//3 here signifies it is an ingredient search
[fetchRequest setPredicate:[self parseSearchObjectsIntoAPredicate:inputCarParts:3]];
//don’t understand these results so comment out
//[fetchRequest setReturnsDistinctResults:YES];
//[fetchRequest setResultType:NSDictionaryResultType];
//attempt to preload the nsset containing the cars (just one each time for some reason
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"ToCar", nil]];
//Perform fetch
NSError *error;
NSArray *records = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
//I have to do this to get the results finally as cars NOT individual instances of car parts
return [self convertToCars:records];
}
-(NSArray*)convertToCars:(NSArray*)records{
NSMutableArray *manipulator =[NSMutableArray arrayWithArray:records];
NSMutableArray *convertedArray =[[NSMutableArray alloc]init];
for (int i=0; i<[manipulator count]; i++) {
//regular way
CarPart *partFromManipulator=(CarPart *)[manipulator objectAtIndex:i];
[convertedArray addObjectsFromArray:[[ partFromManipulator toCar]allObjects]];
}
NSLog(@"Results from core data = %i",[convertedArray count]);
return [NSArray arrayWithArray:convertedArray];
}