Skip navigation.
 
mlRe: Added object to MOC, why isn't this a bug?
FROM : Greg Herlihy
DATE : Thu Jun 01 22:16:11 2006

A set by definition can contain (at most) only one instance of any
particular item. Therefore adding an item to a set that it already belongs
to - effectively does nothing.

Greg


On 6/1/06 8:39 AM, "Bobby B" <<email_removed>> wrote:

> Hey guys;
>
> I was just writing some code to import values into my CoreData model.
> I have the following code, which works fine.  I have three object
> models: for artist, album, and song.  They are connected via
> relationships, etc.  Here's the thing.
>
> If you look in the enumerator, you see that I'm trying to find a
> specific album by an artist.  It works, I can find the album, and then
> add the new song to it.  At the bottom, where I've commented, you see
> that I just "addObject: newAlbum" and then put it back into the
> artists.
>
> It seems like that would *add* an object to the list, so then there
> would be two copies of the same album for that artist, instead of just
> one.  But it doesn't work like that.  It works like I want it to work
> (I just wanted to update the songs for that artist's album, not add a
> second copy of the album.)  I didn't notice this until after I already
> had it running fun.
>
> So, my question is, what is it about CoreData and managed objects that
> I don't see yet that makes this function properly, even though it
> looks like it should be a bug?  Is it something about the actual
> address of the managed object?
>
>
> NSMutableSet *currentAlbumsForArtist = [newArtist
> mutableSetValueForKey:@"albums"];
>
> NSEnumerator *albumEnumerator = [currentAlbumsForArtist objectEnumerator];
> NSManagedObject * currentObject;
> NSManagedObject * newAlbum;
>
> while (currentObject = [albumEnumerator nextObject]) {
>
>      if ([[currentObject valueForKey:@"name"] isEqualToString:albumName]) {
>  newAlbum = currentObject;
>      }
> }
>
> NSMutableSet *currentSongsForAlbum = [newAlbum
> mutableSetValueForKey:@"songs"];
>
> NSManagedObject * newSong = [NSEntityDescription
> insertNewObjectForEntityForName:@"AMPSong"
> inManagedObjectContext:managedObjectContext];
>
> [newSong setValue: songName forKey:@"name"];
> [newSong setValue: location forKey:@"location"];
>
> [currentSongsForAlbum addObject: newSong];
> [newAlbum setValue: currentSongsForAlbum forKey:@"songs"];
>
> // Why isn't this a bug?  It's adding the object, which already exists in
> // the set.  It should have two copies of it now, but it only
> // shows one, whicih is the updated and correct one.
> [currentAlbumsForArtist addObject:newAlbum];
> [newArtist setValue: currentAlbumsForArtist forKey:@"albums"];
>
> Thank you
> Bobby B
>  _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list      (<email_removed>)
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlAdded object to MOC, why isn't this a bug? Bobby B Jun 1, 17:39
mlRe: Added object to MOC, why isn't this a bug? Greg Herlihy Jun 1, 22:16
mlRe: Added object to MOC, why isn't this a bug? Chris Hanson Jun 1, 23:26
mlRe: Added object to MOC, why isn't this a bug? Bobby B Jun 1, 23:33
mlRe: Added object to MOC, why isn't this a bug? Chris Hanson Jun 1, 23:41
mlRe: Added object to MOC, why isn't this a bug? Bobby B Jun 1, 23:54
mlRe: Added object to MOC, why isn't this a bug? Chris Hanson Jun 2, 00:05