• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Added object to MOC, why isn't this a bug?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Added object to MOC, why isn't this a bug?


  • Subject: Re: Added object to MOC, why isn't this a bug?
  • From: Chris Hanson <email@hidden>
  • Date: Thu, 1 Jun 2006 14:26:48 -0700

On Jun 1, 2006, at 8:39 AM, Bobby B wrote:

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;
}
}

Assuming your model is set up appropriately, this could be expressed more succinctly with a fetch request on your Album entity with a predicate such as "(artist == %@) and (name == %@)" (where you pass the artist object and the name when constructing the request).


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"];

Here you're adding the song you just created to the "songs" relationship, and then you're setting the songs relationship again. You don't need to use -setValue:forKey: after modifying the relationship -- you can just modify it, and the modification will be tracked. The mutable set you get back from -mutableSetValueForKey: is the relationship.


// 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"];

It's not a bug because sets don't contain duplicates. Your "newAlbum" isn't actually a new album; it's still the same album, it just has an additional song in it. And just as above, you don't need to set the artist's "albums" relationship again, because by manipulating the mutable set that you got back from - mutableSetValueForKey: you're actually manipulating the relationship.


  -- Chris

_______________________________________________
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


  • Follow-Ups:
    • Re: Added object to MOC, why isn't this a bug?
      • From: "Bobby B" <email@hidden>
References: 
 >Added object to MOC, why isn't this a bug? (From: "Bobby B" <email@hidden>)

  • Prev by Date: Re: Re Re: beginSheet
  • Next by Date: Re: Added object to MOC, why isn't this a bug?
  • Previous by thread: Re: Added object to MOC, why isn't this a bug?
  • Next by thread: Re: Added object to MOC, why isn't this a bug?
  • Index(es):
    • Date
    • Thread