0

I think this is wrong, it only loads one car part: This methods takes two arrays one with car names, one with parts, creates a new car, and adds car parts to it, then saves the car to core data. (currently does not work this way)

for (int i=0; i<[massiveArray count]; i++) {
    //create a new car and part
        Car*newCar =(Car*)[NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:[self managedObjectContext]];
        CarPart *part =[NSEntityDescription insertNewObjectForEntityForName:@"CarPart" inManagedObjectContext:[self managedObjectContext]];

//set car title to string title in array of titles
        [newCar setValue:[massiveArray objectAtIndex:i] forKey:@"name"];
        //go through car parts array and add all new parts for that specific car
        for (int i=0; i<[partNamesArray count]; i++) {
            [part setValue:[partNamesArray objectAtIndex:i] forKey:@"name"];
            [newCar addToCarPartObject:part];
//save each part??? I think this is wrong
            [self.managedObjectContext save:nil];
        }
//Save new car
        [self.managedObjectContext save:nil];
    }
William Falcon
  • 9,813
  • 14
  • 67
  • 110
  • One thing to keep in mind is that when you call save on the managedObjectContext it saves all current pending transactions. It shouldn't actually effect the problem you are seeing, but I would suggest only having one save call that rests outside both loops. But that is an aside, what is currently happening? Does the code run and just not save the objects? EDIT: To clarify my question, does the inner for loop actually fire more than once? – Matt C. Jan 02 '13 at 19:15
  • Inner for loop will fire more than once (a car will have many parts). What is happening is that it is only saving one car part per car, as opposed to all. – William Falcon Jan 02 '13 at 19:21

1 Answers1

2

I know what's going on here.

You need to insert a new part object into core data for each part. As it is right now, you are only making one part object and then overwriting it inside the for loop. Your code should look something akin to this...

for (int i=0; i<[massiveArray count]; i++) {
    //create a new car and part
    Car *newCar = [NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:[self managedObjectContext]];

    //set car title to string title in array of titles
    [newCar setValue:[massiveArray objectAtIndex:i] forKey:@"name"];

    //go through car parts array and add all new parts for that specific car
    for (int i=0; i<[partNamesArray count]; i++) {
        CarPart *part =[NSEntityDescription insertNewObjectForEntityForName:@"CarPart" inManagedObjectContext:[self managedObjectContext]];
        [part setValue:[partNamesArray objectAtIndex:i] forKey:@"name"];
        [newCar addToCarPartObject:part];
    }
}
//Save the entire context (all pending changes to cars and their parts)
[self.managedObjectContext save:nil];

As I said in my above comment, I suggest moving the save to outside the loops.

Another suggestion for easier to read code. When enumerating an array in a simple for loop try something like...

for (NSString *carTitle in massiveArray) {
    /* Now do your stuff in here... 'carTitle' will be different during 
     * each pass of the loop. No need to increment an i variable or grab 
     * the object from the array on each pass. 
     */
}
Matt C.
  • 689
  • 5
  • 14
  • this did the trick, the big thing was saving outside all the for loops – William Falcon Jan 02 '13 at 20:06
  • The big thing should have been creating a new object in core data for each new entry. The saving outside the loops should only have increased execution speed what you had before wasn't technically wrong in that respect. – Matt C. Jan 02 '13 at 20:14
  • it is doing it correct now which you are correct was the big thing. Execution speed is ridiculous fast now thought (thats what was more noticeable) – William Falcon Jan 02 '13 at 20:16