Hi I have 2 entities
- Words
- Tags
Words can have multiple tags and its true with tags, Tags can have multiple words
Question:
- If I add the tag to the word, do I still need to execute the reverse function of it, that is to add the word to tag? I am not sure if I executed 1, core data might automatically execute the reverse.
Detail:
here is the portion of the code when I added the tag to word
rcvTags.forEach { tag in
if let id = tags.firstIndex(where: {$0.name == tag.text}){
// if tag is already existing, just add it to word
newWord.addToWithTagsOf(tags[id])
} else {
// if tag, not yet existing, create the tag
let newTag = Tags(context: self.context)
newTag.name = tag.text
newTag.id = UUID()
// add to the word
newWord.addToWithTagsOf(newTag) //<--- Add tag to word
isTagUpdated = true
}
}
// save context
I think in the Tag entity part, there is also a generated function where I can add the word to tag
extension Tags {
@objc(addWithWordsOfObject:)
@NSManaged public func addToWithWordsOf(_ value: Words). // Add Word to Tag
//.... and so on
}
I was searching the net but I could not find any sample many to many Coredata codes/reference