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];
}
Core Data doesn’t care if you create duplicate entries. There’s no automatic detection of two objects having identical attribute values– because you might want those. If you create two identical
wheelentries, you’ll get two of them.Do you need them? Only you can answer that. It depends on how your app works. Is there ever a situation where those two
wheelobjects might be modified so that they’re not different? Does it make sense to have multiple car objects referencing the same wheel? The answer depends on what your app does and how it uses this data. I don’t know how your app works but my gut feeling is that ifwheelactually refers to a wheel type– e.g. “Bridgestone Blizzak DM-V1 – 245/60R18 105R BSW”, then it makes sense to allow more than one car to use it. If it refers to a specific instance of a wheel– “right-rear tire on this particular car”, then it doesn’t make sense. But only you can say for sure.If you don’t need them, then sure, you can arrange for multiple cars to point to the same wheel. Make the wheel relationship to car a to-many relationship, and then associate one wheel which as many cars as you like. This doesn’t mean Core Data will start automatically detecting duplicate wheels– it never does that. But it does mean that if you keep the same wheel object around, you can use it for more than one car.