0

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];
}
William Falcon
  • 9,813
  • 14
  • 67
  • 110

1 Answers1

1

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 wheel entries, 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 wheel objects 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 if wheel actually 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.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • hi tom, thank you for the input. in your example I want to find all cars that have Bridgestone Blizzak DM-V1 - 245/60R18 105R BSW wheel, so in essence this wouldn't change (it would change at the web level where the data is pulled from, but not in coredata, its read only in core data). I added some code so that you can see what I'm doing (by the way you helped me with this code earlier, but now I want to get rid of duplicates, and end up with an array of cars, although I want to search using the car parts and seems like the only way to have an array of cars is to "convert it" like I did) – William Falcon Jan 08 '13 at 18:33
  • also how do i do this more effectively? if possible after converting back to cars I would like to have the toParts nsset preloaded – William Falcon Jan 08 '13 at 18:36