Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to design multiple tags feature with Core Data?

I have my first iOS app with Core Data, and there is an Entry entity. Entry has the attribute called "Tag" and it's NSString.

So now when user created a new Entry he can put any string into Tag field and it will be stored in Core Data as NSString, which can be used later for search by tag.

The thing is I want to implement multiple tags feature in my app and I can't figure out how to do it, what's the correct design for cases like this in iOS, considering using Core Data.

For example, if someone wants to create an Entry and give it tags like "food", "groceries", "apples". How should I assign all of them to my property of Entry entity? How should I store them in Core Data? As a separate entity Tags with unique ids? How should I retrieve them and how can user edit multiple tags for an Entry?

Thank you in advance for answers.

like image 528
titicaca Avatar asked Sep 05 '25 03:09

titicaca


1 Answers

There are 2 common ways to do that.

  1. the simplest one is to store comma separated tags in your NString. (but you won't be able do filtering and other operations involving tags)

  2. Create another entity - Tag with name and id. And have many-to-many relationship (assuming one tag can be used by several entries) a good explanation on how to do that is given here cdrelationships

like image 114
Tala Avatar answered Sep 07 '25 20:09

Tala