Re: Added object to MOC, why isn't this a bug?
Re: Added object to MOC, why isn't this a bug?
- Subject: Re: Added object to MOC, why isn't this a bug?
- From: Greg Herlihy <email@hidden>
- Date: Thu, 01 Jun 2006 13:16:11 -0700
- Thread-topic: Added object to MOC, why isn't this a bug?
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@hidden> 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@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden