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.
- Should I be using a "one-to-many relationship"?
- How should I handle determining whether an song belongs to a certain album?
- What if their are multiple albums with the same name?
- How do I handle deletion?
If anyone could provide answers, code, or tutorial for any of theseā¦ it would be greatly appreciated.