pierrotlefou
pierrotlefou

Reputation: 40731

core-data : only the last object can be initialized correctly in a loop

My model has two entities Artist and Album, and Album has a Artist instance member. I used following code to pre-populate my model but only found the last Album , that is ablum3, have set up a correct association with the Artist Beatles. For album1,album2, the artist filed are nil.

There must be something wrong which I did not spot...

//create  an artist
NSManagedObject *artist = [NSEntityDescription
                           insertNewObjectForEntityForName:@"Artist"
                           inManagedObjectContext:__managedObjectContext]; 

[artist setValue:@"Beatles" forKey:@"name"];

//populate the data
NSArray *albums = [NSArray arrayWithObjects:@"album1",@"album2",@"album3", nil];
for (NSString *title in albums){
    //populate the data
    NSManagedObject *album = [NSEntityDescription
                              insertNewObjectForEntityForName:@"Album"
                              inManagedObjectContext:__managedObjectContext]; 

    [album setValue:title forKey:@"title"];
    [album setValue:artist forKey:@"artist"];
}

Upvotes: 0

Views: 111

Answers (1)

Lorenzo B
Lorenzo B

Reputation: 33428

Without no further detail, it's difficult to know what is going on. I try to understand the model on what you have written.

So, this model works for me

enter image description here

albums is a to-many relationships to Album. Furthermore it's optional, you can have Artist with no Album.

artist is the inverse rel for Artist. one-to-one cardinality. It's required since you cannot have an Album without an Artist.

Here the code:

- (void)populateDB
{
    //create  an artist
    NSManagedObject *artist = [NSEntityDescription
                               insertNewObjectForEntityForName:@"Artist"
                               inManagedObjectContext:[self managedObjectContext]]; 

    [artist setValue:@"Beatles" forKey:@"name"];

    //populate the data
    NSArray *albums = [NSArray arrayWithObjects:@"album1",@"album2",@"album3", nil];
    for (NSString *title in albums){
        //populate the data
        NSManagedObject *album = [NSEntityDescription
                                  insertNewObjectForEntityForName:@"Album"
                                  inManagedObjectContext:[self managedObjectContext]]; 

        [album setValue:title forKey:@"title"];
        [album setValue:artist forKey:@"artist"];
    }
}

After having called populatedDB, save the context calling [self saveContext]

- (void)saveContext {
    NSError *error = nil;
    NSManagedObjectContext *moc = [self managedObjectContext];
    if (moc != nil) {
        if ([moc hasChanges] && ![moc save:&error]) {
             // Replace this implementation with code to handle the error appropriately.
             // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        } 
    }
}

If you need to arrange your model let me know.

Hope that helps.

Upvotes: 2

Related Questions