0

This is structure of coredata architecture. enter image description here

After adding entries to 'Artists', using them to newly added 'Album' entries is working perfect.

enter image description here

But the problem is as shown in img- 2 & 3, after assigning the 'michael jackson' to 'Insomniac 2010' album & then adding the same artist to 'Baby ft ludacris' losing the reference from the album 'Insomniac'.

This is the code where I save the context in AlbumDetailViewController.h

- (void)EntityRecordstableview:(UITableView *)tableView didselectrowatindexpath:(NSIndexPath *)indexPath forentity:(id)entity
{
    Artist *selectedArtist = entity;

    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelected:NO animated:YES];

    if ([self.pickedArtists containsObject:selectedArtist]) {
        [self.pickedArtists removeObject:selectedArtist];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
        editingAlbum.artist = self.pickedArtists;
        [self saveTheContext:editingAlbum.managedObjectContext];
//        NSLog(@"%d",self.pickedArtists.count);
    }
    else {
        [self.pickedArtists addObject:selectedArtist];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        editingAlbum.artist = self.pickedArtists;
        [self saveTheContext:editingAlbum.managedObjectContext];
//        NSLog(@"%d",self.pickedArtists.count);
    }
}

I think there must be some problem in managedObjectContext saving. Is it should be like this

self.managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
[self saveTheContext:self.managedObjectContext];

instead of

[self saveTheContext:editingAlbum.managedObjectContext];

I tried this but this is also failing to work as per expectations.

You can find my source code here

hp iOS Coder
  • 3,230
  • 2
  • 23
  • 39

2 Answers2

3

I have updated your code. Please check for the same. Their is a problem in relationship between Artist & Album. It must be Many to Many.

Your relationship between Album & Artist is 1 to many. Hence, if you will select Artist for multiple Album then it will override its value. Please check your database for same.

Code : Click Here

MilanPanchal
  • 2,943
  • 1
  • 19
  • 37
  • 1
    Hey thanks bro.! Thanks for updating my code. Its like u made the fruit plate ready for me. :) Thank you very much indeed – hp iOS Coder Apr 09 '13 at 10:35
1

Every artist can have many albums, but according to the model every album can contain many artists. The to-many relation should be reversed or substituted to many-to-many type (Every album can contain many artists and every artist can have many albums)

The album - song relationship type also could be the problem later...

voromax
  • 3,369
  • 2
  • 30
  • 53