1

I am currently working on producing the data model for my application and it is extremely important I get it designed correctly before implementation can begin otherwise it will be a huge problem.

My situation is, I have an entity which is a Post. Each post has many Tags associated with it. Initially, I wanted to store an array of tags within each Post, but then I realised this would not be normalised and there would be repetitive data. As each post may have tags in common with each other.

This led to the creation of the Tag entity. My issue is how do I model the relationship? Many posts are associated with many tags. Each tag may be associated with one or more posts.

Any ideas? Do I need a mapping table, or does Core Data handle this? Thanks.

Tim
  • 8,932
  • 4
  • 43
  • 64

1 Answers1

7

Core Data supports many-to-many relationships so this shouldn't be a problem as long as you define the model correctly. For instance:

enter image description here enter image description here enter image description here

Gobot
  • 2,464
  • 1
  • 21
  • 26
  • 1
    Guess the one thing I'm confused about is how these are represented as NSManagedObjects when I'm populating the database? As I'll be storing posts in their object, but when it comes to set the tags, how will I be able to set multiple tags to one post? And how do I ensure that I don't create two tags with the same data? – Tim Mar 15 '12 at 15:47
  • I think you need take a bit of time to read the apple docs "Core Data Programming Guide": https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/coredata/cdprogrammingguide.html . Then start dev, then go back and read it again. Each time you read it, things start to make sense. – Gobot Mar 15 '12 at 15:55
  • @Gobot can you show an example of how to use Core Data many-to-may relationship? Normally with SQLite, I should create another table call Post-Tag to represent many-to-many relationship – onmyway133 Nov 04 '13 at 04:14
  • @Gobot, how would you store properties that would usually be included in the a Post-Tag join "table"? For example, where would you save tag offset information from a caption, basically the unique properties that are unique to post and tag? – Ryan Romanchuk May 10 '14 at 16:37