Search code examples
iphoneioscore-dataentity-relationship

CoreData programming with multiple Entities


I'm looking for some help with CoreData and with Entities in general. For the purpose of my question, lets say I have a database of albums. I simple have an entity named albums with Attributes for Name, Artist, Year, Album Art, and such.

Now, lets say I want to be able to create songs for this album. I'm thinking I should have a separate Entity for the songs, and have a one-to-many relationship with it. However, I'm not sure exactly how I would do that.

Since I have different albums which contain different songs, I want to separate the songs from AlbumA from Album B. Now, I could have a row in the songs database for which album it belongs to, but is that the most efficient way to do this. What if I have a duplicate album name. I tried creating a unique "hash" of the album name using the time created and the name, but is there a better way.

I also need a better way to handle deletion for when an album is deleted.

Without using one-to-many relationships, I was able to create two separate entities, Albums and Songs, which aren't linked together. However, when I create a new album, it loads a new viewcontroller and passes the "hashes" time-stamped album name to the viewcontroller. Then when I create a new song, it uses that hashed time-stamped album name as a row in the Songs entity. That way, when I'm viewing which songs are in the album, I just set an NSPredicate to only show queries which include the hashes time-stamp. However, deletion is a problem because their are no relationships.

  1. Should I be using a "one-to-many relationship"?
  2. How should I handle determining whether an song belongs to a certain album?
  3. What if their are multiple albums with the same name?
  4. How do I handle deletion?

If anyone could provide answers, code, or tutorial for any of these… it would be greatly appreciated.


Solution

  • So, hopefully this brief answer will get you on track:

    A one-to-many relationship is appropriate for album->songs. As to how to create it - let's assume you have two entities, one called "album" and one called "song". In XCode's data model window, select your 'album' entity and create a new relationship (hit the + button in the properties table).

    Give your relationship a name, decide whether it's optional or not (one would assume not, since every album must have at least one song in it), and select "song" as your destination entity. Finally, click the checkbox marked "To-Many relationship". And that's it!

    Now that you've established a relationship, any songs that are associated with an album will be deleted when you delete the album itself.

    Regarding question 3: "what if there are multiple albums with the same name". This is fairly common scenario. It would be very unusual to use an english string as your primary identifier. You want to have some kind of unique ID for each album - this would be your primary/unique key. If you're confused about what that means you should look into some more basic database concepts to get a better handle on it before moving on with Core Data.